1

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.

Arfons
  • 96
  • 9

1 Answers1

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