So I've seen this question asked several times but there doesn't seem to be a consensus on how it should be done. I have a GUI where a file is created and, on the press of a button, I need to call a separate jar file, utilizing the file name as a parameter for it. Also, I'm utilizing netbeans, if there are any particularities to it and I have the source code to the jar file, although integrating it into my code would be a can of worms.
Asked
Active
Viewed 333 times
1 Answers
2
Just run it using
Runtime.getRuntime().exec("java", "-jar", yourJarName, parameter1, parameter2);
You can pass in an arbitrary number of parameters.
As Malt described below, if you need the output of the resulting process, you can use the following code:
Process p = Runtime.getRuntime().exec("java", "-jar", yourJarName, parameter1, parameter2);
InputStream is = p.getInputStream();
Note that you call InputStream
to get the output of the process, because you are reading from the stream, so it is an input with regards to your program, and an output with regards to the subprocess.

Zoltán
- 21,321
- 14
- 93
- 134
-
That's it? Huh. Oh, also, how would I get its output stream? – Arfons Aug 21 '14 at 14:53
-
2When calling exec() You get a Process object, which has getInputStream() and getOutputStream() methods. – Malt Aug 21 '14 at 14:54
-
Possible duplicate http://stackoverflow.com/questions/4936266/execute-jar-file-from-a-java-program – Thusitha Thilina Dayaratne Aug 21 '14 at 15:04