Determine when a process has ended?

N

news.amnet.net.au

Hi

I am running a process with the runtime command:

Process p = Runtime.getRuntime ().exec (command);

It is very important to me that I know when the process has completed, as
then immediately afterwards I have to do somethign else.
I have tried to get the exitValue() i.e.

int complete = p.exitValue();

But I get an exitvalue of 0 back even before the process completes.
Is there a foolproof way to find out when a process has completed?

My full code of the java program I call from a jsp to start the process is
like this:

package geoinformex.process;
import java.io.*;
import java.net.*;
import java.util.*;

public class RunProcess {

Process p;

public Process RunProcess (String command , boolean wait) {

try
{
p = Runtime.getRuntime ().exec (command);
}
catch ( IOException e )
{
return null;
}
if ( wait )
{
try
{
p.waitFor();
}
catch ( InterruptedException e )
{
Thread.currentThread().interrupt();
}
}
return p;
}
}

Then in my jsp I do this:

String command = "some command here";
boolean wait = false;

RunProcess proc = new RunProcess();
proc.RunProcess(command,wait);

int exitvalue = proc.exitValue();

But, as I said, this exitValue() immediately returns 0, even before the
process completes.

Any help with determining when a process has completed will be very welcome.

Thanks very much

Hugo
 
D

David Roden

news.amnet.net.au said:
String command = "some command here";
boolean wait = false;

If you want to wait for the end of the process, why don't you say so?
 
C

Chris Smith

news.amnet.net.au said:
I am running a process with the runtime command:

Process p = Runtime.getRuntime ().exec (command);

It is very important to me that I know when the process has completed, as
then immediately afterwards I have to do somethign else.
I have tried to get the exitValue() i.e.

int complete = p.exitValue();

But I get an exitvalue of 0 back even before the process completes.
Is there a foolproof way to find out when a process has completed?

You'll have to use the waitFor method. If you need to find this out
without waiting for the process to complete, you may need to create a
new Thread to wait for the process to complete, and then set a boolean
flag somewhere and Object.notify() or some such thing.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top