5
\$\begingroup\$

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();
}
\$\endgroup\$
  • \$\begingroup\$ The standard way of handling this is you distribute you code as a zip. User runs the bin/run.bat, bin/launch.exe whatever. If he somehow invokes Foobar.main in lib/foobar.jar in some other way, maybe he knows what he is doing. Just ensure you have the parameters and let anything else slide. \$\endgroup\$ – abuzittin gillifirca Mar 25 at 8:43
3
\$\begingroup\$

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());
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.