tags:

views:

304

answers:

5

Edit: It seems my test to determine whether the original JVM had exited was flawed to begin with (see comments on accepted answer). Sorry for the noise.

I have a need to have a running JVM start another JVM and then exit. I'm currently trying to do this via Runtime.getRuntime().exec(). The other JVM starts, but my original JVM won't exit until the "child" JVM process stops. It appears that using Runtime.getRuntime().exec() creates a parent-child relationship between the processes. Is there some way to de-couple the spawned process so that the parent can die, or some other mechanism to spawn a process without any relationship to the creating process?

Note that this seems exactly like this question: http://stackoverflow.com/questions/2566502/using-java-to-spawn-a-process-and-keep-it-running-after-parent-quits but the accepted answer there doesn't actually work, at least not on my system (Windows 7, Java 5 and 6). It seems that maybe this is a platform-dependent behavior. I'm looking for a platform independent way to reliably invoke the other process and let my original process die.

For example, suppose I have a jar file at C:\myjar.jar and I want to run the class com.example.RunMe that lives in that jar. Lets say that class pops up a JOptionPane, and then exits once the user has hit OK.

Now, the following is the program running in JVM #1:

public static void main(String[] args) {
    String javaHome = System.getProperty("java.home");
    String os = System.getProperty("os.name");
    String javawBin = javaHome + File.separator + "bin" + File.separator + "javaw";

    if (os.toLowerCase().contains("win")) {
        javawBin += ".exe";
    }

    List<String> cmd = new ArrayList<String>();

    cmd.add("\"" + javawBin + "\"");
    cmd.add("-cp");
    cmd.add("\"C:\\myjar.jar\"");
    cmd.add("com.example.RunMe");

    System.out.println("Running: " + cmd);

    try {

        System.out.println("Launching...");
        Process p = Runtime.getRuntime().exec(cmd.toArray(new String[cmd.size()]));

        new Thread(new StreamGobbler(p.getInputStream())).start();
        new Thread(new StreamGobbler(p.getErrorStream())).start();

        System.out.println("Launched JVM.");

        System.exit(0);

    } catch (IOException e) {
        e.printStackTrace();
    }

}

private static class StreamGobbler implements Runnable {
    InputStream stream;

    StreamGobbler(InputStream stream) {
        this.stream = stream;
    }

    public void run() {
        byte[] buf = new byte[64];

        try {
            while (stream.read(buf) != -1)
                ;
        } catch (IOException e) {

        }
    }
}

The observed behavior is that both "Launching..." and "Launched JVM." are printed, but JVM #1 only exits after you hit OK in the JOptionPane launched by JVM #2. Also - the behavior is the same whether or not you start up the stream gobbler threads or not.

Also, to save someone the breath, yes I know I could create a new URLClassLoader with that jar file and run it that way, but thats not what I'm trying to do here.

A: 

As far as I know, killing a process fairly often kills all child processes. I doubt there's a platform independent way to do this.

esalaka
2 things: 1) Can a process spawn another, independent process that isn't a child? and 2) I _can't_ kill the parent at all. System.exit(0) hangs until the child is stopped.
CarlG
A: 

Windows doesn't establish the same kind of parent-child relationship between processes that Unix systems do. It is likely that your parent process isn't exiting because there's a thread still running in it. This thread may be waiting for the child process to terminate, which could explain why your parent exits when the child exits.

Greg Hewgill
The parent process hangs on System.exit(0). Wouldn't that pre-empt waiting for a thread to finish? Also, is this still true in Win7?
CarlG
@CarlG: Hanging in `System.exit(0)` is definitely unexpected. I wouldn't imagine that the semantics of process termination have changed in Windows 7, but I guess they could have. I'm curious to see what the eventual resolution is for this one.
Greg Hewgill
+1  A: 

I just tried the following code, and I see processes being spawned and main one exiting on Vista and Java 6. I think something else might be going on with your code.

public class Test {
    public static void main(String[] args) throws Exception {
        if(args.length == 0)
            Runtime.getRuntime().exec("javaw Test loop");
        else
            while(true){}
    }
}
ykaganovich
I think you're right. Heres the thing. My barometer to check whether or not JVM #1 has exited was whether or not the red "stop" button was active in the Eclipse console, as this has been a reliable indicator of JVM life in the past. However, the Windows task manager is showing me a different story, namely that JVM #1 does exit immediately, as expected. How would that eclipse stop button be tied to the child process, then?
CarlG
Maybe Eclipse doesn't understand System.exit?
ykaganovich
All eclipse is doing is spawning javaw.exe itself. There's got to be some sort of link its hanging on to? It is odd that it disappears from the task manager though. Maybe I've made a mountain out of a molehill.
CarlG
Eclipse messes with the input/output too, making use of `Console`, which registers a shutdownhook.
Stephen Denne
Yeah, I've been testing it using actual jar'ed versions using the command line directly, and it appears to work just fine. Sorry for the noise everyone.
CarlG
Or rather - not providing a Console.
Stephen Denne
A: 

Your threads running StreamGobblers are within Process #1, and are not daemon threads, so Process #1 doesn't end till those threads complete, when the Streams they are gobbling go away as Process #2 ends.

Take out the two lines that create those threads.

Stephen Denne
That was my first instinct, but I would think System.exit(0) should take care of running threads. But reading Runtime.exit documentation, I wonder if a different error code would result in a different behavior.
ykaganovich
Reasonable, but as I indicate above, taking out those 2 lines has no effect.
CarlG
Right, sorry. Calling `System.exit(0)` takes care of those threads.
Stephen Denne
A: 

Hi...Guys.......i searched lot and atlast i found the best way to spawn process is using ant tool...

kosal
Huh? This seems completely off-topic, not to mention incomplete.
CarlG
sorry...........
kosal