In addition to the points mentioned in @l0b0 @l0b0's answer...
Getting the OS
It's somewhat weird that you only get the first five letters from System.getProperty("os.name")
to determine if it's Linux or Windo [sic]. Also, instead of repeatedly calling confstring()
(which doesn't really explain what it is doing, perhaps call it getOSName()
?), you should consider putting it into a static final
variable, so that it can be referenced easily multiple times:
public class Organizer {
private static final String OS_NAME = System.getProperty("os.name").toUpperCase();
public static void main(String[] args) {
String command = OS_NAME.startsWith("WINDOWS") ? "CMD" : "bash";
String commandArg = OS_NAME.startsWith("WINDOWS") ? "/C" : "/c";
// ...
}
}
Actually, an enum
-driven approach might be worth considering as well:
enum OS {
LINUX("bash", "-c"),
WINDOWS("CMD", "/C");
public static final OS CURRENT = System.getProperty("os.name")
.toUpperCase().startsWith("WINDOWS")
? WINDOWS : LINUX;
private final String command;
private final String commandArg;
private OS(String command, String commandArg) {
this.command = command;
this.commandArg = commandArg;
}
// for illustration, you should be using getters for command and commandArg
public String toString() {
return command + " " + commandArg;
}
}
With the example above, calling System.out.println(OS.CURRENT)
will print "CMD /C"
on a Windows OS. An enum
is also quite suitable in this case, where we have a pre-defined set of possible values to specify for. For example, if you want to use a different shell for Mac OS X (or macOS), you can do so by adding a new value and checking for it when deriving the value for OS.CURRENT
.
In addition to the points mentioned in @l0b0's answer...
Getting the OS
It's somewhat weird that you only get the first five letters from System.getProperty("os.name")
to determine if it's Linux or Windo [sic]. Also, instead of repeatedly calling confstring()
(which doesn't really explain what it is doing, perhaps call it getOSName()
?), you should consider putting it into a static final
variable, so that it can be referenced easily multiple times:
public class Organizer {
private static final String OS_NAME = System.getProperty("os.name").toUpperCase();
public static void main(String[] args) {
String command = OS_NAME.startsWith("WINDOWS") ? "CMD" : "bash";
String commandArg = OS_NAME.startsWith("WINDOWS") ? "/C" : "/c";
// ...
}
}
Actually, an enum
-driven approach might be worth considering as well:
enum OS {
LINUX("bash", "-c"),
WINDOWS("CMD", "/C");
public static final OS CURRENT = System.getProperty("os.name")
.toUpperCase().startsWith("WINDOWS")
? WINDOWS : LINUX;
private final String command;
private final String commandArg;
private OS(String command, String commandArg) {
this.command = command;
this.commandArg = commandArg;
}
// for illustration, you should be using getters for command and commandArg
public String toString() {
return command + " " + commandArg;
}
}
With the example above, calling System.out.println(OS.CURRENT)
will print "CMD /C"
on a Windows OS. An enum
is also quite suitable in this case, where we have a pre-defined set of possible values to specify for. For example, if you want to use a different shell for Mac OS X (or macOS), you can do so by adding a new value and checking for it when deriving the value for OS.CURRENT
.
In addition to the points mentioned in @l0b0's answer...
Getting the OS
It's somewhat weird that you only get the first five letters from System.getProperty("os.name")
to determine if it's Linux or Windo [sic]. Also, instead of repeatedly calling confstring()
(which doesn't really explain what it is doing, perhaps call it getOSName()
?), you should consider putting it into a static final
variable, so that it can be referenced easily multiple times:
public class Organizer {
private static final String OS_NAME = System.getProperty("os.name").toUpperCase();
public static void main(String[] args) {
String command = OS_NAME.startsWith("WINDOWS") ? "CMD" : "bash";
String commandArg = OS_NAME.startsWith("WINDOWS") ? "/C" : "/c";
// ...
}
}
Actually, an enum
-driven approach might be worth considering as well:
enum OS {
LINUX("bash", "-c"),
WINDOWS("CMD", "/C");
public static final OS CURRENT = System.getProperty("os.name")
.toUpperCase().startsWith("WINDOWS")
? WINDOWS : LINUX;
private final String command;
private final String commandArg;
private OS(String command, String commandArg) {
this.command = command;
this.commandArg = commandArg;
}
// for illustration, you should be using getters for command and commandArg
public String toString() {
return command + " " + commandArg;
}
}
With the example above, calling System.out.println(OS.CURRENT)
will print "CMD /C"
on a Windows OS. An enum
is also quite suitable in this case, where we have a pre-defined set of possible values to specify for. For example, if you want to use a different shell for Mac OS X (or macOS), you can do so by adding a new value and checking for it when deriving the value for OS.CURRENT
.
In addition to the points mentioned in @l0b0's answer...
Getting the OS
It's somewhat weird that you only get the first five letters from System.getProperty("os.name")
to determine if it's Linux or Windo [sic]. Also, instead of repeatedly calling confstring()
(which doesn't really explain what it is doing, perhaps call it getOSName()
?), you should consider putting it into a static final
variable, so that it can be referenced easily multiple times:
public class Organizer {
private static final String OS_NAME = System.getProperty("os.name").toUpperCase();
public static void main(String[] args) {
String command = OS_NAME.startsWith("WINDOWS") ? "CMD" : "bash";
String commandArg = OS_NAME.startsWith("WINDOWS") ? "/C" : "/c";
// ...
}
}
Actually, an enum
-driven approach might be worth considering as well:
enum OS {
LINUX("bash", "-c"),
WINDOWS("CMD", "/C");
public static final OS CURRENT = System.getProperty("os.name")
.toUpperCase().startsWith("WINDOWS")
? WINDOWS : LINUX;
private final String command;
private final String commandArg;
private OS(String command, String commandArg) {
this.command = command;
this.commandArg = commandArg;
}
// for illustration, you should be using getters for command and commandArg
public String toString() {
return command + " " + commandArg;
}
}
With the example above, calling System.out.println(OS.CURRENT)
will print "CMD /C"
on a Windows OS. An enum
is also quite suitable in this case, where we have a pre-defined set of possible values to specify for. For example, if you want to use a different shell for Mac OS X (or macOS), you can do so by adding a new value and checking for it when deriving the value for OS.CURRENT
.