Execute another jar in a Java program

winsontan520 picture winsontan520 · Aug 24, 2009 · Viewed 119k times · Source

I had written several simple java applications named as A.jar, B.jar.

Now i want to write a GUI java program so that user can press button A to execute A.jar and button B to execute B.jar.

Also i want to output the run-time process detail in my GUI program.

Any suggestion?

Answer

gjrwebber picture gjrwebber · Aug 24, 2009

If I understand correctly it appears you want to run the jars in a separate process from inside your java GUI application.

To do this you can use:

// Run a java app in a separate system process
Process proc = Runtime.getRuntime().exec("java -jar A.jar");
// Then retreive the process output
InputStream in = proc.getInputStream();
InputStream err = proc.getErrorStream();

Its always good practice to buffer the output of the process.