public class ProcessExecutor
extends java.lang.Object
This class uses the CommonControls thread pool to monitor the process's input and output streams, and allows the caller to easily parse the outputs by using a visitor pattern.
Modifier and Type | Class and Description |
---|---|
static class |
ProcessExecutor.BufferingVisitor
A visitor that just buffers the output of the process and makes it
available for retrieval.
|
static interface |
ProcessExecutor.LineVisitor
Interface defining a visitor that will be called with lines of text
read from the process output.
|
static interface |
ProcessExecutor.Visitor
Interface defining a visitor that will be called with data read
from the child process's output.
|
Constructor and Description |
---|
ProcessExecutor(java.lang.String... cmd)
Creates a new executor for the given process with the given arguments.
|
Modifier and Type | Method and Description |
---|---|
ProcessExecutor |
addCurrentEnv()
Adds the current process's environment to the child.
|
ProcessExecutor |
addEnv(java.lang.String name,
java.lang.String value)
Adds a specific environment to use with the child process.
|
ProcessExecutor |
addVisitor(ProcessExecutor.LineVisitor v)
Adds a visitor instance to use with the child process.
|
ProcessExecutor |
addVisitor(ProcessExecutor.Visitor v)
Adds a visitor to monitor the child process's output.
|
int |
exitValue()
Returns the exit value of the child process.
|
java.lang.String[] |
getCmd()
Returns the command and arguments it is supposed to execute
|
ProcessExecutor |
setDirectory(java.lang.String cwd)
Sets the working directory for the child process.
|
java.lang.Process |
start()
Executes the child commmand, returning a process on success.
|
int |
waitFor()
Waits for execution of the child process to finish.
|
public ProcessExecutor(java.lang.String... cmd)
cmd
- The command to execute. The first argument is the
executable, which should be an absolute path, or be in
the user's PATH. The remaining are arguments to the
executable.public ProcessExecutor setDirectory(java.lang.String cwd)
dir
- Path to the working directory.public java.lang.String[] getCmd()
public ProcessExecutor addCurrentEnv()
This allows the current environment to be inherited by the child
process, while still allowing customization using
addEnv(String, String)
.
public ProcessExecutor addEnv(java.lang.String name, java.lang.String value)
Calling this method makes the child ignore the current process's
environment. See addCurrentEnv()
for how to inherit the
current environment.
name
- Name of variable to set.value
- Value of variable.public ProcessExecutor addVisitor(ProcessExecutor.Visitor v)
v
- A visitor.public ProcessExecutor addVisitor(ProcessExecutor.LineVisitor v)
v
- The visitor to use.public java.lang.Process start() throws java.io.IOException
java.io.IOException
public int waitFor() throws java.lang.InterruptedException
java.lang.InterruptedException
public int exitValue()