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!

[433 byte] By [hballenchena] at [2007-11-27 11:52:52]
# 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

java_2006a at 2007-7-29 18:47:04 > top of Java-index,Desktop,Runtime Environment...
# 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.

hballenchena at 2007-7-29 18:47:04 > top of Java-index,Desktop,Runtime Environment...