Link to home
Start Free TrialLog in
Avatar of BrianMc1958
BrianMc1958

asked on

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
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You'd be better simply ensuring that the main thread of the class run finishes. If it doesn't, the process will run 'forever'
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
Avatar of BrianMc1958
BrianMc1958

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If your app won't exit 'naturally' you'll have to call Process.destroy
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

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

:-)

You could try to dump the threads to see what's happening (ctrl-pause[break])
if new process is running in javaw, how can you dump threads? in which window should i press the keys?
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
My question to BrianMc1958  was why did he think the caller also had a thread in the called process.