|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.zeroturnaround.exec.ProcessExecutor
public class ProcessExecutor
Helper for executing sub processes.
It's implemented as a wrapper of ProcessBuilder complementing it with additional features such as:
execute()) or returning a Future (start().readOutput(boolean), ProcessResult).The default configuration for executing a process is following:
redirectErrorStream(boolean) to override it.NullOutputStream, Use streams(ExecuteStreamHandler), redirectOutput(OutputStream),
or any of the redirectOutputAs* methods.to override it.exitValues(Integer...) to override it.Process.destroy() is invoked.
ProcessResult| Field Summary | |
|---|---|
static Integer[] |
DEFAULT_EXIT_VALUES
|
static boolean |
DEFAULT_REDIRECT_ERROR_STREAM
|
| Constructor Summary | |
|---|---|
ProcessExecutor()
Creates new ProcessExecutor instance. |
|
ProcessExecutor(Iterable<String> command)
Creates new ProcessExecutor instance for the given program and its arguments. |
|
ProcessExecutor(List<String> command)
Creates new ProcessExecutor instance for the given program and its arguments. |
|
ProcessExecutor(String... command)
Creates new ProcessExecutor instance for the given program and its arguments. |
|
| Method Summary | |
|---|---|
ProcessExecutor |
addDestroyer(ProcessDestroyer destroyer)
Adds a process destroyer to be notified when the process starts and stops. |
ProcessExecutor |
addListener(ProcessListener listener)
Register new process event handler. |
ProcessExecutor |
clearListeners()
Unregister all existing process event handlers. |
ProcessExecutor |
closeTimeout(long timeout,
TimeUnit unit)
Sets a timeout for closing standard streams of the process being executed. |
ProcessExecutor |
command(Iterable<String> command)
Sets the program and its arguments which are being executed. |
ProcessExecutor |
command(List<String> command)
Sets the program and its arguments which are being executed. |
ProcessExecutor |
command(String... command)
Sets the program and its arguments which are being executed. |
ProcessExecutor |
commandSplit(String commandWithArgs)
Splits string by spaces and passes it to command(String...)NB: this method do not handle whitespace escaping, "mkdir new\ folder" would be interpreted as
{"mkdir", "new\", "folder"} command. |
ProcessExecutor |
debug()
Deprecated. use redirectOutput(OutputStream) and Slf4jStream |
ProcessExecutor |
debug(org.slf4j.Logger log)
Deprecated. use redirectOutput(OutputStream) and Slf4jStream |
ProcessExecutor |
debug(String name)
Deprecated. use redirectOutput(OutputStream) and Slf4jStream |
ProcessExecutor |
destroyer(ProcessDestroyer destroyer)
Sets the process destroyer to be notified when the process starts and stops. |
ProcessExecutor |
destroyOnExit()
Sets the started process to be destroyed on VM exit (shutdown hooks are executed). |
ProcessExecutor |
directory(File directory)
Sets this working directory for the process being executed. |
ProcessExecutor |
environment(Map<String,String> env)
Adds additional environment variables for the process being executed. |
ProcessExecutor |
environment(String name,
String value)
Adds a single additional environment variable for the process being executed. |
ProcessResult |
execute()
Executes the sub process. |
ProcessResult |
executeNoTimeout()
Executes the sub process. |
ProcessExecutor |
exitValue(Integer exitValue)
Sets the allowed exit value for the process being executed. |
ProcessExecutor |
exitValueAny()
Allows any exit value for the process being executed. |
ProcessExecutor |
exitValueNormal()
Allows only 0 as the exit value for the process being executed. |
ProcessExecutor |
exitValues(int[] exitValues)
Sets the allowed exit values for the process being executed. |
ProcessExecutor |
exitValues(Integer... exitValues)
Sets the allowed exit values for the process being executed. |
ProcessExecutor |
info()
Deprecated. use redirectOutput(OutputStream) and Slf4jStream |
ProcessExecutor |
info(org.slf4j.Logger log)
Deprecated. use redirectOutput(OutputStream) and Slf4jStream |
ProcessExecutor |
info(String name)
Deprecated. use redirectOutput(OutputStream) and Slf4jStream |
ProcessExecutor |
listener(ProcessListener listener)
Unregister all existing process event handlers and register new one. |
PumpStreamHandler |
pumps()
|
ProcessExecutor |
readOutput(boolean readOutput)
Sets this process executor's readOutput property. |
ProcessExecutor |
redirectError(OutputStream output)
Redirects the process' error stream to given output stream. |
ProcessExecutor |
redirectErrorAlsoTo(OutputStream output)
Redirects the process' error stream also to a given output stream. |
ProcessExecutor |
redirectErrorAsDebug()
Deprecated. use redirectError(OutputStream) and Slf4jStream |
ProcessExecutor |
redirectErrorAsDebug(org.slf4j.Logger log)
Deprecated. use redirectError(OutputStream) and Slf4jStream |
ProcessExecutor |
redirectErrorAsDebug(String name)
Deprecated. use redirectError(OutputStream) and Slf4jStream |
ProcessExecutor |
redirectErrorAsInfo()
Deprecated. use redirectError(OutputStream) and Slf4jStream |
ProcessExecutor |
redirectErrorAsInfo(org.slf4j.Logger log)
Deprecated. use redirectError(OutputStream) and Slf4jStream |
ProcessExecutor |
redirectErrorAsInfo(String name)
Deprecated. use redirectError(OutputStream) and Slf4jStream |
ProcessExecutor |
redirectErrorStream(boolean redirectErrorStream)
Sets this process executor's redirectErrorStream property. |
ProcessExecutor |
redirectInput(InputStream input)
Sets the input stream to redirect to the process' input stream. |
ProcessExecutor |
redirectOutput(OutputStream output)
Redirects the process' output stream to given output stream. |
ProcessExecutor |
redirectOutputAlsoTo(OutputStream output)
Redirects the process' output stream also to a given output stream. |
ProcessExecutor |
redirectOutputAsDebug()
Deprecated. use redirectOutput(OutputStream) and Slf4jStream |
ProcessExecutor |
redirectOutputAsDebug(org.slf4j.Logger log)
Deprecated. use redirectOutput(OutputStream) and Slf4jStream |
ProcessExecutor |
redirectOutputAsDebug(String name)
Deprecated. use redirectOutput(OutputStream) and Slf4jStream |
ProcessExecutor |
redirectOutputAsInfo()
Deprecated. use redirectOutput(OutputStream) and Slf4jStream |
ProcessExecutor |
redirectOutputAsInfo(org.slf4j.Logger log)
Deprecated. use redirectOutput(OutputStream) and Slf4jStream |
ProcessExecutor |
redirectOutputAsInfo(String name)
Deprecated. use redirectOutput(OutputStream) and Slf4jStream |
ProcessExecutor |
removeListener(ProcessListener listener)
Unregister existing process event handler. |
ProcessExecutor |
removeListeners(Class<? extends ProcessListener> listenerType)
Unregister existing process event handlers of given type or its sub-types. |
ProcessExecutor |
setMessageLogger(MessageLogger messageLogger)
Changes how most common messages about starting and waiting for processes are actually logged. |
StartedProcess |
start()
Start the sub process. |
ProcessExecutor |
stopper(ProcessStopper stopper)
Sets the helper for stopping the process in case of timeout or cancellation. |
ExecuteStreamHandler |
streams()
|
ProcessExecutor |
streams(ExecuteStreamHandler streams)
Sets a stream handler for the process being executed. |
ProcessExecutor |
timeout(long timeout,
TimeUnit unit)
Sets a timeout for the process being executed. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final Integer[] DEFAULT_EXIT_VALUES
public static final boolean DEFAULT_REDIRECT_ERROR_STREAM
| Constructor Detail |
|---|
public ProcessExecutor()
ProcessExecutor instance.
public ProcessExecutor(List<String> command)
ProcessExecutor instance for the given program and its arguments.
command - The list containing the program and its arguments.public ProcessExecutor(String... command)
ProcessExecutor instance for the given program and its arguments.
command - A string array containing the program and its arguments.public ProcessExecutor(Iterable<String> command)
ProcessExecutor instance for the given program and its arguments.
command - The iterable containing the program and its arguments.| Method Detail |
|---|
public ProcessExecutor command(List<String> command)
command - The list containing the program and its arguments.
public ProcessExecutor command(String... command)
command - A string array containing the program and its arguments.
public ProcessExecutor command(Iterable<String> command)
command - The iterable containing the program and its arguments.
public ProcessExecutor commandSplit(String commandWithArgs)
command(String...)"mkdir new\ folder" would be interpreted as
{"mkdir", "new\", "folder"} command.
commandWithArgs - A string array containing the program and its arguments.
public ProcessExecutor directory(File directory)
null -- this means to use the
working directory of the current Java process, usually the
directory named by the system property user.dir,
as the working directory of the child process.
directory - The new working directory
public ProcessExecutor environment(Map<String,String> env)
env - environment variables added to the process being executed.
public ProcessExecutor environment(String name,
String value)
name - name of the environment variable added to the process being executed.value - value of the environment variable added to the process being executed.
public ProcessExecutor redirectErrorStream(boolean redirectErrorStream)
redirectErrorStream property.
If this property is true, then any error output generated by subprocesses will be merged with the standard output.
This makes it easier to correlate error messages with the corresponding output.
The initial value is true.
redirectErrorStream - The new property value
public ProcessExecutor exitValueAny()
public ProcessExecutor exitValueNormal()
0 as the exit value for the process being executed.
public ProcessExecutor exitValue(Integer exitValue)
exitValue - single exit value or null if all exit values are allowed.
public ProcessExecutor exitValues(Integer... exitValues)
exitValues - set of exit values or null if all exit values are allowed.
public ProcessExecutor exitValues(int[] exitValues)
exitValues - set of exit values or null if all exit values are allowed.
public ProcessExecutor timeout(long timeout,
TimeUnit unit)
TimeoutException is thrown and the process is destroyed.
This only applies to execute methods not start methods.
timeout - timeout for running a process.
public ProcessExecutor stopper(ProcessStopper stopper)
By default DestroyProcessStopper is used which just invokes Process.destroy().
stopper - helper for stopping the process (null means NopProcessStopper - process is not stopped).
public ExecuteStreamHandler streams()
public ProcessExecutor streams(ExecuteStreamHandler streams)
public ProcessExecutor closeTimeout(long timeout,
TimeUnit unit)
This can be used on Windows in case a process exits quickly but closing the streams blocks forever.
Closing timeout must fit into the general execution timeout (see timeout(long, TimeUnit)).
By default there's no closing timeout.
timeout - timeout for closing streams of a process.
public ProcessExecutor redirectInput(InputStream input)
input - input stream that will be written to the process input stream (null means nothing will be written to the process input stream).
public ProcessExecutor redirectOutput(OutputStream output)
redirectOutputAlsoTo(OutputStream) if you want to redirect the output to multiple streams.
output - output stream where the process output is redirected to (null means NullOutputStream which acts like a /dev/null).
public ProcessExecutor redirectError(OutputStream output)
redirectErrorAlsoTo(OutputStream) if you want to redirect the error to multiple streams.
Calling this method automatically disables merging the process error stream to its output stream.
output - output stream where the process error is redirected to (null means NullOutputStream which acts like a /dev/null).
public ProcessExecutor redirectOutputAlsoTo(OutputStream output)
public ProcessExecutor redirectErrorAlsoTo(OutputStream output)
Calling this method automatically disables merging the process error stream to its output stream.
public PumpStreamHandler pumps()
null).
IllegalStateException - if the current stream handler is not an instance of PumpStreamHandler.streams()public ProcessExecutor readOutput(boolean readOutput)
readOutput property.
If this property is true,
the process output should be read to a buffer and returned by ProcessResult.output().
The initial value is false.
readOutput - The new property value
public ProcessExecutor info(org.slf4j.Logger log)
redirectOutput(OutputStream) and Slf4jStream
Logger with info level.
public ProcessExecutor debug(org.slf4j.Logger log)
redirectOutput(OutputStream) and Slf4jStream
Logger with debug level.
public ProcessExecutor info(String name)
redirectOutput(OutputStream) and Slf4jStream
Logger with given name using info level.
public ProcessExecutor debug(String name)
redirectOutput(OutputStream) and Slf4jStream
Logger with given name using debug level.
public ProcessExecutor info()
redirectOutput(OutputStream) and Slf4jStream
Logger of the caller class using info level.
public ProcessExecutor debug()
redirectOutput(OutputStream) and Slf4jStream
Logger of the caller class using debug level.
public ProcessExecutor redirectOutputAsInfo(org.slf4j.Logger log)
redirectOutput(OutputStream) and Slf4jStream
Logger with info level.
public ProcessExecutor redirectOutputAsDebug(org.slf4j.Logger log)
redirectOutput(OutputStream) and Slf4jStream
Logger with debug level.
public ProcessExecutor redirectOutputAsInfo(String name)
redirectOutput(OutputStream) and Slf4jStream
Logger with given name using info level.
public ProcessExecutor redirectOutputAsDebug(String name)
redirectOutput(OutputStream) and Slf4jStream
Logger with given name using debug level.
public ProcessExecutor redirectOutputAsInfo()
redirectOutput(OutputStream) and Slf4jStream
Logger of the caller class using info level.
public ProcessExecutor redirectOutputAsDebug()
redirectOutput(OutputStream) and Slf4jStream
Logger of the caller class using debug level.
public ProcessExecutor redirectErrorAsInfo(org.slf4j.Logger log)
redirectError(OutputStream) and Slf4jStream
Logger with info level.
public ProcessExecutor redirectErrorAsDebug(org.slf4j.Logger log)
redirectError(OutputStream) and Slf4jStream
Logger with debug level.
public ProcessExecutor redirectErrorAsInfo(String name)
redirectError(OutputStream) and Slf4jStream
Logger with given name using info level.
public ProcessExecutor redirectErrorAsDebug(String name)
redirectError(OutputStream) and Slf4jStream
Logger with given name using debug level.
public ProcessExecutor redirectErrorAsInfo()
redirectError(OutputStream) and Slf4jStream
Logger of the caller class using info level.
public ProcessExecutor redirectErrorAsDebug()
redirectError(OutputStream) and Slf4jStream
Logger of the caller class using debug level.
public ProcessExecutor addDestroyer(ProcessDestroyer destroyer)
destroyer - helper for destroying all processes on certain event such as VM exit (not null).
public ProcessExecutor destroyer(ProcessDestroyer destroyer)
This methods always removes any other ProcessDestroyer registered. Use addDestroyer(ProcessDestroyer) to keep the existing ones.
destroyer - helper for destroying all processes on certain event such as VM exit (maybe null).
public ProcessExecutor destroyOnExit()
To undo this command call destroyer(null).
public ProcessExecutor listener(ProcessListener listener)
listener - process event handler to be set (maybe null).
public ProcessExecutor addListener(ProcessListener listener)
listener - process event handler to be added.
public ProcessExecutor removeListener(ProcessListener listener)
listener - process event handler to be removed.
public ProcessExecutor removeListeners(Class<? extends ProcessListener> listenerType)
listenerType - process event handler type.
public ProcessExecutor clearListeners()
public ProcessExecutor setMessageLogger(MessageLogger messageLogger)
MessageLoggers.DEBUG is used.
However if someone is executing a process every second MessageLoggers.TRACE may be used e.g.
messageLogger - message logger for certain level.
public ProcessResult execute()
throws IOException,
InterruptedException,
TimeoutException,
InvalidExitValueException
IOException - an error occurred when process was started or stopped.
InterruptedException - this thread was interrupted.
TimeoutException - timeout set by timeout(long, TimeUnit) was reached.
InvalidExitValueException - if invalid exit value was returned (@see exitValues(Integer...)).
public ProcessResult executeNoTimeout()
throws IOException,
InterruptedException,
InvalidExitValueException
timeout(long, TimeUnit) is ignored (use execute() for timeout).
IOException - an error occurred when process was started or stopped.
InterruptedException - this thread was interrupted.
InvalidExitValueException - if invalid exit value was returned (@see exitValues(Integer...)).
public StartedProcess start()
throws IOException
timeout(long, TimeUnit) is ignored.
Use Future.get() to wait for the process to finish.
Invoke future.cancel(true); to destroy the process.
IOException - an error occurred when process was started.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||