Socratic Phoenix
Socratic Phoenix

Reputation: 556

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?

Upvotes: 0

Views: 386

Answers (3)

Stephen C
Stephen C

Reputation: 718718

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.

Upvotes: 2

tom
tom

Reputation: 1493

You may want to check out Apache Commons Exec. Here is a question related to it. How to run a java program using apache commons-exec?

Upvotes: 0

14bmkelley
14bmkelley

Reputation: 17

Yes, this works for most linux, mac and windows. I don't really know it gets hairy or not on more obscure linux platforms though.

Linux: https://askubuntu.com/questions/101746/how-can-i-execute-a-jar-file-from-the-terminal

Mac: Executing a jar on mac 10.8

Sorry for the kind of lame articles, but they illustrate the point.

Upvotes: 0

Related Questions