0

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?

Socratic Phoenix
  • 556
  • 1
  • 7
  • 19

3 Answers3

2

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thanks for your answer, however: – Socratic Phoenix Jun 27 '15 at 03:58
  • woops hit post by accident, -- continuing, however, 1. I know that my user must have java on the path, 2. I'm gonna compile in like, 1.5, so most versions should work, 3. that was just testing code, I will use File.seperator, 4. it is critically it also works on linux so... so to conclude, due to the environment the users have, I can make those assumptions about the java installation. – Socratic Phoenix Jun 27 '15 at 04:00
0

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.

Community
  • 1
  • 1
14bmkelley
  • 17
  • 5
0

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?

Community
  • 1
  • 1
tom
  • 1,331
  • 1
  • 15
  • 28