How to know if a process has ended?

Hi !

I have started a process with the Runtime.exec() method and I need to know if it running. The API specification says that the Process.exitValue() returns an int when the process is dead, or throws an exception when it is alive. It works in my development environment (VM1.2.2) but when I make it run outside (VM1.3.1) it returns 0 when the process is still alive.

What can I do?

Thx for your ideas,

Manuel

[448 byte] By [godbert] at [2007-9-26 20:48:23]
# 1

I can't find any documented change in the manner in which the JVM handles the exitValue, the only thing I can think of is to wait until the process has terminated by using the waitFor method. For example:

Process proc = Runtime.getRuntime().exec(cmd);

try {

proc.waitFor();

}

catch (InterruptedException e) {

System.err.println("process was interrupted");

}

if (proc.exitValue() != 0) System.err.println("exit value was non-zero");

V.V.

viravan at 2007-7-3 19:39:54 > top of Java-index,Desktop,Runtime Environment...
# 2

The problem is that I do not want to wait for the process to end. Actually I need to know if the process has ended to know if I have a chance getting some valuable information by reading from its output stream.

Thanks for contributing but I need another way to get through.

Manuel

godbert at 2007-7-3 19:39:55 > top of Java-index,Desktop,Runtime Environment...
# 3
Finally I managed to do what I wanted to, with a second thread that does the waitFor().Your idea was the good idea, I just needed to use it the right way, thx.
godbert at 2007-7-3 19:39:55 > top of Java-index,Desktop,Runtime Environment...
# 4
How to know if a process has ended and if process not finished yet, How can I kill a process?
iwoqweasdzxc at 2007-7-3 19:39:55 > top of Java-index,Desktop,Runtime Environment...