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:
Your
ProcessBuilder
will not work ifjava
is not on the search path.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.You are using a pathname for the JAR file with windows syntax. That won’t work on other platforms.
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 usejavaw
instead. But thenjavaw
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.