views:

432

answers:

3

Hi, guys, I encounter a confusing issue! I want to invoke a executable JAR to compute PI from my main java applicaion using runtime.exec(), which create a new JVM for running the executable JAR. My issue is that when the PI computation is done, the JVM is still alive. my main java application has no idea whether PI computation is finished or not. And I also want when the PI computation is done, the JVM could be shutdown! How can I implement that! ThankS!!

+2  A: 

When you call Runtime.exec() you will get a Process object back. You need to call waitFor() on this.

You will also need to capture the stdout/stderr streams (in separate threads to prevent blocking - see this answer for more info).

This all leaves aside why you're doing this in a separate JVM, and why you can't load the relevant classes into your current app and run the library locally.

Brian Agnew
Yes, I used waitFor(), but I think process.waitFor() in another thread is blocking until the JVM is terminated. But actually, when I use Runtime.exec() invoke a executable JAR, when JAR finish, the JVM is still alive, so the waitFor() is always blocking! that is the problem I met! What I wonder is how could terminate the JVM when the JAR running on it finished??
Johnson
Are you collecting stdout/stderr in a non-blocking fashion ? And does executing the .jar standalone result in blocking ?
Brian Agnew
Actually, I donot want to receive stdout from the PI computation. I just want it runs standlone and automatically save the result in a .txt file which all completed by the JAR own, and finally, the JVM could be teminated automatically without bothering the main application!
Johnson
I don't think that answered either of my questions
Brian Agnew
ThX for Ur patience:) I try to make my question simple! when I run this PI Calculation JAR in pure JVM, which I mean, cmd: java -jar ... it works very well, and when the computation finishes, JVM is teminated as well! But when I run the same jar through exec(), and use process.waitFor() to wait the compuation end, but it keeps alive! That is what confuses me! I hope you can help me figure it out!:)
Johnson
You need to read your process stdout/stderr. If you don't then your subprocess will block.
Brian Agnew
haha, the problem solved! just change the jre! I used JBuilder build-in jre, the jar's JVM did not quit! When I change to java jdk's jre, the problme solved:) thanks for your help!
Johnson
A: 

To answer the second part of your question, the JVM always exits when no non-daemon threads are still running. Or in plainer speech, when your application is "done" and the main method exits without leaving any threads running in the background, the JVM will finish.

This is true regardless of whether you launch the Java process yourself from your desktop/command line, or fire it off via Runtime.exec() (which is broadly equivalent). So when your Pi calculation terminates it will shut down the JVM you spawned, and when your original program finishes then its JVM will also exit.

Though I agree completely with Brian here, I can't see the benefit in running a Java app as a separate process when you should be able to just run it in the original JVM (barring some really unusual environmental stuff such as setting niceness or processor affinity of the various processes).

Andrzej Doyle
the reason I run PI calculation in seperate JVM is that if the PI calculation run out of Virtual memory or cause the JVM down, my main application will still work! I try to build a worker application in a grid computing architecture! In order to secure the worker application, I have to run the task separately:)
Johnson
A: 

check what the jar does when you run it standalone, maybe its waiting for input and therefor it will never exit

Salandur
I have run this JAR in standlone JVM, it works and when it has done, the JVM also is teminated! but if I invoke it in exec(), and use process.waitFor(), it never ends! :(
Johnson