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?

2
  • Hardcoding '/' or '\' in file path is not recommended. Use system property File.separator instead. Commented Jun 27, 2015 at 3:22
  • Its just test code.... Im gonna switch Commented Jun 27, 2015 at 4:00

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

Comments

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.