Package com.day.util
Class ProcessRunner
- java.lang.Object
-
- com.day.util.ProcessRunner
-
- All Implemented Interfaces:
java.lang.Runnable
public class ProcessRunner extends java.lang.Object implements java.lang.Runnable
TheProcessRunner
class helps running external processes. This encompasses redirection of stdin, stdout and stderr as well as optionally waiting for the completion of the process. This implementation is based on theRuntime.exec(String)
method and does not yet support passing specific environments or decomposed comand lines.This class can be used in two ways. If you don't care about waiting for the processes completion and return code, you might do it like this :
Runnable pr = new ProcessRunner("command", null, null, null);
new Thread(pr).start();If on the other hand you want to capture all output and also keep an eye on the time the process takes to execute (or wait indefinitely), you might use the class like this :
ProcessRunner pr = new ProcessRunner("command", in, out, err);
pr.run(0);
int rc = pr.getReturnCode();- Since:
- coati Audience wad
-
-
Field Summary
Fields Modifier and Type Field Description static int
PROCESS_ABORTED
Constant to indicate the process has been abortedstatic int
PROCESS_RUNNING
Constant to indicate the process is running
-
Constructor Summary
Constructors Constructor Description ProcessRunner(java.lang.String cmdLine)
Creates a newProcessRunner
to execute the given command line containing the command to execute and all relevant command arguments with no redirection of stdin, stdout and stderr.ProcessRunner(java.lang.String cmdLine, java.io.InputStream stdin, java.io.OutputStream stdout, java.io.OutputStream stderr)
Creates a newProcessRunner
to execute the given command line containing the command to execute and all relevant command arguments.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
getReturnCode()
Returns the return code from running the command line.void
run()
Runs the process sending the input data and capturing output data.void
run(long waitTime)
Executes the command line and waits for the completion of the prcoess.
-
-
-
Field Detail
-
PROCESS_RUNNING
public static final int PROCESS_RUNNING
Constant to indicate the process is running- See Also:
- Constant Field Values
-
PROCESS_ABORTED
public static final int PROCESS_ABORTED
Constant to indicate the process has been aborted- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ProcessRunner
public ProcessRunner(java.lang.String cmdLine, java.io.InputStream stdin, java.io.OutputStream stdout, java.io.OutputStream stderr)
Creates a newProcessRunner
to execute the given command line containing the command to execute and all relevant command arguments. The stdinInputStream
can be used to feed input data to the command executed, while the output (stdout and stderr) are redirected to the givenOutputStream
s or the process defaults (System.out
andSystem.err
, resp.).None of the streams is closed after running the command line. This is the sole duty of the client of this class.
- Parameters:
cmdLine
- This commandline is given to theRuntime.exec(String)
contains all the command arguments and is split up with aStringTokenizer
. See the API docs on theRuntime.exec(String)
method for details.stdin
- TheInputStream
containing data to be handed to the process as stdin. Set this tonull
if the process should not get any input. This stream is completely read after the process is started and sent to the process. Therefor the stream should be available.stdout
- TheOutputStream
to send the stdout output of the process to. If this isnull
, stdout output is written toSystem.out
.stderr
- TheOuputStream
to send the stderr output of the process to. If this isnull
, stderr output is written toSystem.err
.
-
ProcessRunner
public ProcessRunner(java.lang.String cmdLine)
Creates a newProcessRunner
to execute the given command line containing the command to execute and all relevant command arguments with no redirection of stdin, stdout and stderr.- Parameters:
cmdLine
- This commandline is given to theRuntime.exec(String)
contains all the command arguments and is split up with aStringTokenizer
. See the API docs on theRuntime.exec(String)
method for details.
-
-
Method Detail
-
run
public void run(long waitTime)
Executes the command line and waits for the completion of the prcoess. The process runs in its own thread which is marked as daemon thread. That is as soon as the Java VM is about to end, the process will also forcibly stopped and does not run to completion.- Parameters:
waitTime
- The number of milliseconds to wait for the completion of the process. If the time has ellapsed before the process has terminated, the process will be forcibly terminated. If this value is negative, the process runs completely detached, while a value of zero indicates to wait indeterminate for the completion of the process.
-
getReturnCode
public int getReturnCode()
Returns the return code from running the command line. As long as the command is running, the method returnsPROCESS_RUNNING
. After the process has terminated, the method returns the return code from the process orPROCESS_ABORTED
if the command has been terminated due to a timeout.- Returns:
- The return code from the process or either
PROCESS_RUNNING
orPROCESS_ABORTED
.
-
run
public void run()
Runs the process sending the input data and capturing output data.Do not directly call this method. Instead either use
new Thread(new ProcessRunner(cmd)).start()
or use therun(long)
method.- Specified by:
run
in interfacejava.lang.Runnable
-
-