Problems about invoke another java application in current JVM
I want invoke another java application dynamcally through user defined jar and arguments in the same JVM as my application running.
I can find the main method from the jar file, and invoke it successfully. But there are two questions:
1. When the invoked application exits, my application will exit too.
2. Can I capture the invoked application's stdout and stderr outputs?
Please help me. Thank you!
# 1
read this five pages article
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=1
basically, you'll need to execute a jar using Runtime.exec("java -jar theOtherJar.jar") and capture its output as follows:
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("dir");
InputStream stdin = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(stdin);
BufferedReader br = new BufferedReader(isr);
hth
Message was edited by:
java_2006
# 2
Thank you.
Your proposed method does work.
But there are two questions too.
1. Runtime.exec is OS dependent for the command line.
2. It runs in another JVM.
Anyway, it is a solution. Thank you very much.