Package com.day.util
Class ProcessRunner
java.lang.Object
com.day.util.ProcessRunner
- All Implemented Interfaces:
Runnable
The
ProcessRunner 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
the Runtime.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
FieldsModifier and TypeFieldDescriptionstatic final intConstant to indicate the process has been abortedstatic final intConstant to indicate the process is running -
Constructor Summary
ConstructorsConstructorDescriptionProcessRunner(String cmdLine) Creates a newProcessRunnerto execute the given command line containing the command to execute and all relevant command arguments with no redirection of stdin, stdout and stderr.ProcessRunner(String cmdLine, InputStream stdin, OutputStream stdout, OutputStream stderr) Creates a newProcessRunnerto execute the given command line containing the command to execute and all relevant command arguments. -
Method Summary
-
Field Details
-
PROCESS_RUNNING
public static final int PROCESS_RUNNINGConstant to indicate the process is running- See Also:
-
PROCESS_ABORTED
public static final int PROCESS_ABORTEDConstant to indicate the process has been aborted- See Also:
-
-
Constructor Details
-
ProcessRunner
Creates a newProcessRunnerto execute the given command line containing the command to execute and all relevant command arguments. The stdinInputStreamcan be used to feed input data to the command executed, while the output (stdout and stderr) are redirected to the givenOutputStreams or the process defaults (System.outandSystem.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- TheInputStreamcontaining data to be handed to the process as stdin. Set this tonullif 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- TheOutputStreamto send the stdout output of the process to. If this isnull, stdout output is written toSystem.out.stderr- TheOuputStreamto send the stderr output of the process to. If this isnull, stderr output is written toSystem.err.
-
ProcessRunner
Creates a newProcessRunnerto 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 Details
-
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_ABORTEDif the command has been terminated due to a timeout.- Returns:
- The return code from the process or either
PROCESS_RUNNINGorPROCESS_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.
-