Skip to content
Advertisement

Java Command Line on Different OS’s

Okay, so I am using process builder to launch an independent java process from the current java process, using the code:

ProcessBuilder pb = new ProcessBuilder("java", "-jar", "C:\Users\MyName\Desktop\Test.jar");
    pb.start();

to test it, just as a simple questin, will the command always be “java -jar something.jar,” on all operating systems? and if not, what are the formats for mac and linux?

Advertisement

Answer

The answer is complicated. Some of the complications are:

  1. Your ProcessBuilder will not work if java is not on the search path.

  2. Your ProcessBuilder will give you the version of java on the search path, which may be different to the version that you want to use; e.g. if the user has installed multiple versions of java.

  3. You are using a pathname for the JAR file with windows syntax. That won’t work on other platforms.

  4. You are using the java launcher. On Windows that will launch the JVM with its own console window. (And that is rather crude / ugly.) You may want to use javaw instead. But then javaw only exists on Windows.


TL;DR – what you have written will work (with some modifications) if you make some assumptions about the Java installation, but those assumptions are not always valid.


My suggestion would be to launch a shell script or batch file that runs the JAR file, and provide different versions for different platforms. Do it in a way that allows the administrator / expert user tweak the script to address issues relevant to their deployment of your software.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement