Java program that relaunches itself in OS's terminal application I wrote a Java program that when its executable .jar is opened manually, it re-opens itself inside the operating system's shell console. The purpose of this is to have a C#-style console application where the user can enter commands, without the user needing to manually open the shell console and run the java -jar ... command to open the application.

Would you consider this a good implementation?

Would you make a custom console GUI instead of using the OS's console?

public static void main(String[] args){
    if(args.length == 0){ // then re-open in console with argument to indicate that there's no need to re-open next time.
        try {
            String os = System.getProperty("os.name").toLowerCase();
            String path = new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getAbsolutePath();
            if(os.contains("mac")){
                String command = "tell application \"Terminal\"\n" + 
                        "do script \"java -jar \'" + path + "\' console\"\n" + 
                        "activate\n" + 
                        "end tell";
                Runtime.getRuntime().exec( new String[]{"osascript", "-e", command} );
                System.exit(0);
            } else if(os.contains("win")) {
                Runtime.getRuntime().exec("cmd /c start cmd.exe /K java -jar \"" + path + "\" console");
                System.exit(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }
    }
    System.out.println("Enter command.  'help' for list of commands.");
    while(true)
        nextCommand();
}
A few empty lines here and there would do wonders to the readability.



This will also relaunch the application if it was started from a terminal without arguments.



String path = new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getAbsolutePath();




String os = System.getProperty("os.name").toLowerCase();
...
if(os.contains("mac")){


Unfortunately that is a quite fragile way to detect the current operating system, but it will do.



On nay other operating system, you simply start reading commands (or not), are you aware of that?

You should have the command reading in an else branch.



System.exit(0);


Please stop sprinkling your code with those. System.exit makes the JVM exit immediately, it is like a SIGKILL on UNIX. That means that not even final branches are being run when invoked.



while(true)
    nextCommand();


I assume that there is a "quit" command which calls System.exit, that might be a bad idea. A better idea would be to have something like this:

while(commandExecutor.isActive()) {
    commandExecutor.readAndExecuteCommand());
}