Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2005-05-13
12
Medium Priority
?
284 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:BrianMc1958
  • 6
  • 4
  • 2
12 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 13996600
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 13996624
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
 

Author Comment

by:BrianMc1958
ID: 13996787
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

Accepted Solution

by:
CEHJ earned 900 total points
ID: 13996869
What happens when you run it manually from the command line using java (not javaw)?
0
 
LVL 2

Assisted Solution

by:limaideal
limaideal earned 600 total points
ID: 13996964
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 13996988
If your app won't exit 'naturally' you'll have to call Process.destroy
0
 

Author Comment

by:BrianMc1958
ID: 13998569
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
 
LVL 2

Expert Comment

by:limaideal
ID: 13998632
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 13998687
:-)

You could try to dump the threads to see what's happening (ctrl-pause[break])
0
 
LVL 2

Expert Comment

by:limaideal
ID: 13998767
if new process is running in javaw, how can you dump threads? in which window should i press the keys?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13998799
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
 
LVL 2

Expert Comment

by:limaideal
ID: 13998849
My question to BrianMc1958  was why did he think the caller also had a thread in the called process.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses
Course of the Month12 days, 9 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question