• Post Reply Bookmark Topic Watch Topic
  • New Topic

Process waitFor ?  RSS feed

 
Ec Kjellberg
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for these beginners / stupid question but my java patience has expired...

I have a java application that I want to do some fancy commands (like "mysql -uteligent -pteligent -X -e \"show processlist\"") and it do it very well when I run it by itself on my machine.

Code snippet:

Process proc = Runtime.getRuntime().exec(exec);
proc.waitFor();
int rc = proc.exitValue();
analyse(proc.getInputStream());

But then I start another java application (like JBoss) and while it is running I run my java application (from another commandline screen) and then it hangs on the proc.waitFor() line!

Why?

I simply removed the waitFor() and the exitValue() lines and then I could run my application fine (and read from proc.getInputStream() to analyse the output from proc).

If I only remove the waitFor then I get IllegalThreadStateException from proc.exitValue() saying 'process has not exited'

Is it typical for Windows (XP) that I'm running?

TIA / Ecka
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to read the output before the process will terminate. You're assuming all the output from the process will be saved somewhere for you to read later, but that's not how it works; if you want to save the output, then you need to do that yourself by reading it all and then storing it someplace.

You can't ask for the exit status until the process has exited; you have to call waitFor to wait for that to happen.
 
Ec Kjellberg
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK - I got the order wrong (of course - cannot read output after process is terminated - make sense)
If I did

Process proc = Runtime.getRuntime().exec(exec);
analyse(proc.getInputStream());
proc.waitFor();
int rc = proc.exitValue();

then everything works!
Thanx/E
 
Nikhil Jain
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am facing a similar problem.I am trying to execute some external process with Process.getRuntime.exec. I am also using a java Time with this. The main idea is to see if the process does not execute within certain seconds, the java time api calles the process.destroy. But I see that, when I call destroy, the process does waits for some time, after calling it.

Am I doing right, or is there any other way to destroy the process after a specified time.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To the original post ... if your process writes to errout, you will need to read that stream to completion, too. Because it might alternate lines to stdout and errout, you pretty much have to read them both on independent threads. With everything on separate threads, I found that waitFor() returned before the streams ran out. There must be some buffering in there somewhere, but not enough to avoid your original problem.

To Shashank, I'm not sure what happens in destroy(). It could be the other process is in the middle of something that's not interruptable, or should we expect the OS to be able to kill the process no matter what it's doing?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!