HELP! How to know when a Runtime-invoked JVM has finished with it's class? HELP!

Dear Experts,

(This is a follow-up...)

I am trying to use Runtime to invoke another class, using an independent-process javaw.exe.  However, when the called class finishes (either normally or abnormally), the called JVM continues to run.  Meanwhile, my calling class is waiting for the called class to finish, but it doesn't know it's finished, because the called JVM continues to run.  Is there any way for me to know when the called class is finished?    

This from the calling class:

    Runtime r = Runtime.getRuntime();
    Process p = null;
    p = r.exec("javaw.exe MyCalledClass");
    p.waitFor();
    System.out.Println("Called class died"); // IT NEVER GETS HERE

and this is from the called class:

    if (someError)
      {
        System.exit(0);
      }

and I have tried:

    if (someError)
      {
         int x = 1;
         int y = 0;
         x = x / y;      
      }

I don't want to implement runnable at this point.  I want two separate JVM's running.

Any help would be greatly appreciated.

Thanks,
BrianMc1958
BrianMc1958Asked:
Who is Participating?
 
CEHJCommented:
What happens when you run it manually from the command line using java (not javaw)?
0
 
CEHJCommented:
You'd be better simply ensuring that the main thread of the class run finishes. If it doesn't, the process will run 'forever'
0
 
CEHJCommented:
Calling System.exit(0)  would be a way of doing that when there *isn't* an error. If there is, it shouldn't return 0
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
BrianMc1958Author Commented:
Dear CEHJ,

This is why I'm so confused.  I have tried hard-coding the called class to do all of the following:

--end normally
--end with System.exit(0)
--end with uncaught exceptions

In all cases, the called JVM continues to run, even though the called class stops.  I am (nearly) certain that when I last visited this issue last year, the called JVM WOULD stop when the called class stopped.  What's up?

--BrianMc1958
0
 
limaidealCommented:
do u have a separate thread running in the called app? Or do you have another frame or panel openning( might not be visible). Those will prevent JVM from exiting. as CEHJ suggested, run the called app manually,  check TaskManager to see if the JVM is still there or not.
0
 
CEHJCommented:
If your app won't exit 'naturally' you'll have to call Process.destroy
0
 
BrianMc1958Author Commented:
When I run it from the command line, it DOES end (both the class, and the JVM).  I believe I now know what is happening.  I think that when you use Runtime, the CALLING class ITSELF is regarded as a thread by the CALLED JVM.  Therefore, when you do System.exit(0) in the called class, the JVM does not go away because it thinks it still has an operating thread (the CALLING class).  I should emphasize that I am NOT an expert, and this is just my theory.  Yet it seems to fit with what I am seeing.

Thanks again for all you help, folks!

--BrianMc1958

0
 
limaidealCommented:
BrianMc1958 ,

How do you know the calling class has a thread in the called JVM? It sounds a little strange to me. Do you have any proof?

If so, how did you resolve the problem?

I have done lots of coding in invoking another process(either a new JVM or a thridparty application), never got the problem you mentioned.

Oh, one more posibility, do you have output to stderr or stdout in the called app? Those stuff could stuck the output stream.

Li

0
 
CEHJCommented:
:-)

You could try to dump the threads to see what's happening (ctrl-pause[break])
0
 
limaidealCommented:
if new process is running in javaw, how can you dump threads? in which window should i press the keys?
0
 
CEHJCommented:
That's a point - it's really the sub-process that needs to be dumped, so you'd have to do it programmatically in some way
0
 
limaidealCommented:
My question to BrianMc1958  was why did he think the caller also had a thread in the called process.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.