public class ProcessRunner
extends java.lang.Object
implements java.lang.Runnable
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();
Modifier and Type | Field and Description |
---|---|
static int |
PROCESS_ABORTED
Constant to indicate the process has been aborted
|
static int |
PROCESS_RUNNING
Constant to indicate the process is running
|
Constructor and Description |
---|
ProcessRunner(java.lang.String cmdLine)
Creates a new
ProcessRunner 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 new
ProcessRunner to execute the given command
line containing the command to execute and all relevant command
arguments. |
Modifier and Type | Method and 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.
|
public static final int PROCESS_RUNNING
public static final int PROCESS_ABORTED
public ProcessRunner(java.lang.String cmdLine, java.io.InputStream stdin, java.io.OutputStream stdout, java.io.OutputStream stderr)
ProcessRunner
to execute the given command
line containing the command to execute and all relevant command
arguments. The stdin InputStream
can be used to feed
input data to the command executed, while the output (stdout and stderr)
are redirected to the given OutputStream
s or the process
defaults (System.out
and System.err
, resp.).
None of the streams is closed after running the command line. This is the sole duty of the client of this class.
cmdLine
- This commandline is given to the
Runtime.exec(String)
contains all the command
arguments and is split up with a StringTokenizer
.
See the API docs on the Runtime.exec(String)
method
for details.stdin
- The InputStream
containing data to be handed
to the process as stdin. Set this to null
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
- The OutputStream
to send the stdout output
of the process to. If this is null
, stdout output
is written to System.out
.stderr
- The OuputStream
to send the stderr output
of the process to. If this is null
, stderr output
is written to System.err
.public ProcessRunner(java.lang.String cmdLine)
ProcessRunner
to execute the given command
line containing the command to execute and all relevant command
arguments with no redirection of stdin, stdout and stderr.cmdLine
- This commandline is given to the
Runtime.exec(String)
contains all the command
arguments and is split up with a StringTokenizer
.
See the API docs on the Runtime.exec(String)
method
for details.public void run(long waitTime)
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.public int getReturnCode()
PROCESS_RUNNING
. After
the process has terminated, the method returns the return code from the
process or PROCESS_ABORTED
if the command has been terminated
due to a timeout.PROCESS_RUNNING
or PROCESS_ABORTED
.public void run()
Do not directly call this method. Instead either use
new Thread(new ProcessRunner(cmd)).start()
or use the
run(long)
method.
run
in interface java.lang.Runnable
"Copyright © 2010 - 2020 Adobe Systems Incorporated. All Rights Reserved"