Need info about Runtime.exec
Hello, I have a non program specific question about how Runtime.exec works. First off assume that I handle all my input streams and output streams correctly, and I am not worried about inter process communication. The question is, when I use Runtime.getRuntime().exec(cmd[],pth[],dir)
does the JVM that is running the code that calls it live until the spawned process dies, or can it die without waiting for the execed process to die? I am trying to write an app that can spawn a new copy of itself that is completely independent from the original process, and may live on long after the original is no longer needed. To ensure proper resource management I need to know that the JVM that launches the new process does not have to wait for the new process to die prior to completely cleaning itself up.
Does that make sense?
Any suggestions, ideas, questions?
Thanks in advance for the help.
Message was edited by:
elixic
[965 byte] By [
elixica] at [2007-11-26 14:54:00]

I believe that you'll find it varies by os; the correct solution is to test it for yourself.
In Windows XP, the exec'd program and the Java thread are dependent - one dies (or ends), the other does too. However, if the exec'd program spawns another program, that program is no longer (necessarily) coupled with the Java thread, and may continue to live (or die) independent of what Java does, and vice versa.
Note: the above is not guaranteed!
On Linux I have tested a code where the JVM terminates after spawning the other process.
The following code was tried:
import java.io.IOException;
public class TestExec {
public static void main(String[] args) throws IOException {
Runtime.getRuntime().exec("gedit");
}
}
here the JVM terminates while gedit is still running.
> n Windows XP, the exec'd program and the Java thread are dependent - one dies (or ends), the other does too
This is not true. Child program will live as well but he will lose access to console. Check processes using task manager or similar application - you'll see. There is easier way - start not console but GUI program. Notepad.exe is good enough. Will notepad disappear after JVM exit?
Thank you all who gave useful input. I had done some reading on this already and could not find a definite answer on weather or not the calling JVM can properly dispose of itself prior to the process it called being disposed of.
The answer it seems is dependent on context. In windows, and from a GUI app, it looks like the calling JVM can die any time it wants too. A command line app on the other hand seems to have to wait for the app it called to die first. I have not tested it on other OS's but I am sure it varies by OS as well. I would not even be surprised if it varies by JVM.
So the short answer to my question that you all helped me find is this, It varies by context.
Thanks again for the help.
Isaac