Class ProcessExecutor
It's implemented as a wrapper of ProcessBuilder complementing it with additional features such as:
- Handling process streams (copied from Commons Exec library).
- Destroying process on VM exit (copied from Commons Exec library).
- Checking process exit code.
- Setting a timeout for running the process and automatically stopping it in case of timeout.
- Either waiting for the process to finish (
execute()) or returning aFuture(start(). - Reading the process output stream into a buffer (
readOutput(boolean),ProcessResult).
The default configuration for executing a process is following:
- Process is not automatically destroyed on VM exit.
- Error stream is redirected to its output stream. Use
redirectErrorStream(boolean)to override it. - Output stream is pumped to a
NullOutputStream, Usestreams(ExecuteStreamHandler),redirectOutput(OutputStream), or any of theredirectOutputAs*methods.to override it. - Any exit code is allowed. Use
exitValues(Integer...)to override it. - In case of timeout or cancellation
Process.destroy()is invoked.
- Author:
- Rein Raudjärv
- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates newProcessExecutorinstance.ProcessExecutor(Iterable<String> command) Creates newProcessExecutorinstance for the given program and its arguments.ProcessExecutor(String... command) Creates newProcessExecutorinstance for the given program and its arguments.ProcessExecutor(List<String> command) Creates newProcessExecutorinstance for the given program and its arguments. -
Method Summary
Modifier and TypeMethodDescriptionaddDestroyer(ProcessDestroyer destroyer) Adds a process destroyer to be notified when the process starts and stops.addListener(ProcessListener listener) Register new process event handler.voidcheckExitValue(ProcessResult result) Check the exit value of given process result.Unregister all existing process event handlers.closeTimeout(long timeout, TimeUnit unit) Sets a timeout for closing standard streams of the process being executed.Sets the program and its arguments which are being executed.Sets the program and its arguments which are being executed.Sets the program and its arguments which are being executed.commandSplit(String commandWithArgs) Splits string by spaces and passes it tocommand(String...)
NB: this method do not handle whitespace escaping,"mkdir new\ folder"would be interpreted as{"mkdir", "new\", "folder"}command.debug()Deprecated.Deprecated.debug(org.slf4j.Logger log) Deprecated.destroyer(ProcessDestroyer destroyer) Sets the process destroyer to be notified when the process starts and stops.Sets the started process to be destroyed on VM exit (shutdown hooks are executed).Sets this working directory for the process being executed.environment(String name, String value) Adds a single additional environment variable for the process being executed.environment(Map<String, String> env) Adds additional environment variables for the process being executed.execute()Executes the sub process.Executes the sub process.Sets the allowed exit value for the process being executed.Allows any exit value for the process being executed.Allows only0as the exit value for the process being executed.exitValues(int[] exitValues) Sets the allowed exit values for the process being executed.exitValues(Integer... exitValues) Sets the allowed exit values for the process being executed.Returns this process executor's operating system program and arguments.Returns this process executor's working directory.Returns this process executor's additional environment variables.info()Deprecated.Deprecated.info(org.slf4j.Logger log) Deprecated.protected <T> Future<T> invokeSubmit(ExecutorService executor, Callable<T> task) Override this to customize how the waiting task is started in the background.listener(ProcessListener listener) Unregister all existing process event handlers and register new one.protected ExecutorServicenewExecutor(String processName) pumps()readOutput(boolean readOutput) Sets this process executor'sreadOutputproperty.redirectError(OutputStream output) Redirects the process' error stream to given output stream.redirectErrorAlsoTo(OutputStream output) Redirects the process' error stream also to a given output stream.Deprecated.redirectErrorAsDebug(String name) Deprecated.redirectErrorAsDebug(org.slf4j.Logger log) Deprecated.Deprecated.redirectErrorAsInfo(String name) Deprecated.redirectErrorAsInfo(org.slf4j.Logger log) Deprecated.redirectErrorStream(boolean redirectErrorStream) Sets this process executor'sredirectErrorStreamproperty.redirectInput(InputStream input) Sets the input stream to redirect to the process' input stream.redirectOutput(OutputStream output) Redirects the process' output stream to given output stream.redirectOutputAlsoTo(OutputStream output) Redirects the process' output stream also to a given output stream.Deprecated.redirectOutputAsDebug(String name) Deprecated.redirectOutputAsDebug(org.slf4j.Logger log) Deprecated.Deprecated.redirectOutputAsInfo(String name) Deprecated.redirectOutputAsInfo(org.slf4j.Logger log) Deprecated.removeListener(ProcessListener listener) Unregister existing process event handler.removeListeners(Class<? extends ProcessListener> listenerType) Unregister existing process event handlers of given type or its sub-types.setMessageLogger(MessageLogger messageLogger) Changes how most common messages about starting and waiting for processes are actually logged.start()Start the sub process.protected final org.zeroturnaround.exec.WaitForProcessStart the process and its stream handlers.stopper(ProcessStopper stopper) Sets the helper for stopping the process in case of timeout or cancellation.streams()streams(ExecuteStreamHandler streams) Sets a stream handler for the process being executed.Sets a timeout for the process being executed.protected <T> Callable<T> Override this to customize how the background task is created.
-
Field Details
-
DEFAULT_EXIT_VALUES
-
DEFAULT_REDIRECT_ERROR_STREAM
public static final boolean DEFAULT_REDIRECT_ERROR_STREAM- See Also:
-
-
Constructor Details
-
ProcessExecutor
public ProcessExecutor()Creates newProcessExecutorinstance. -
ProcessExecutor
Creates newProcessExecutorinstance for the given program and its arguments.- Parameters:
command- The list containing the program and its arguments.
-
ProcessExecutor
Creates newProcessExecutorinstance for the given program and its arguments.- Parameters:
command- A string array containing the program and its arguments.
-
ProcessExecutor
Creates newProcessExecutorinstance for the given program and its arguments.- Parameters:
command- The iterable containing the program and its arguments.- Since:
- 1.8
-
-
Method Details
-
getCommand
-
command
Sets the program and its arguments which are being executed.- Parameters:
command- The list containing the program and its arguments.- Returns:
- This process executor.
-
command
Sets the program and its arguments which are being executed.- Parameters:
command- A string array containing the program and its arguments.- Returns:
- This process executor.
-
command
Sets the program and its arguments which are being executed.- Parameters:
command- The iterable containing the program and its arguments.- Returns:
- This process executor.
- Since:
- 1.8
-
commandSplit
Splits string by spaces and passes it tocommand(String...)
NB: this method do not handle whitespace escaping,"mkdir new\ folder"would be interpreted as{"mkdir", "new\", "folder"}command.- Parameters:
commandWithArgs- A string array containing the program and its arguments.- Returns:
- This process executor.
-
getDirectory
Returns this process executor's working directory. Subprocesses subsequently started by this object will use this as their working directory. The returned value may benull-- this means to use the working directory of the current Java process, usually the directory named by the system propertyuser.dir, as the working directory of the child process.- Returns:
- this process executor's working directory
-
directory
Sets this working directory for the process being executed. The argument may benull-- this means to use the working directory of the current Java process, usually the directory named by the system propertyuser.dir, as the working directory of the child process.- Parameters:
directory- The new working directory- Returns:
- This process executor.
-
getEnvironment
-
environment
Adds additional environment variables for the process being executed.- Parameters:
env- environment variables added to the process being executed.- Returns:
- This process executor.
-
environment
Adds a single additional environment variable for the process being executed.- Parameters:
name- name of the environment variable added to the process being executed.value- value of the environment variable added to the process being executed.- Returns:
- This process executor.
- Since:
- 1.7
-
redirectErrorStream
Sets this process executor'sredirectErrorStreamproperty.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 istrue.- Parameters:
redirectErrorStream- The new property value- Returns:
- This process executor.
-
exitValueAny
Allows any exit value for the process being executed.- Returns:
- This process executor.
-
exitValueNormal
Allows only0as the exit value for the process being executed.- Returns:
- This process executor.
-
exitValue
Sets the allowed exit value for the process being executed.- Parameters:
exitValue- single exit value ornullif all exit values are allowed.- Returns:
- This process executor.
-
exitValues
Sets the allowed exit values for the process being executed.- Parameters:
exitValues- set of exit values ornullif all exit values are allowed.- Returns:
- This process executor.
-
exitValues
Sets the allowed exit values for the process being executed.- Parameters:
exitValues- set of exit values ornullif all exit values are allowed.- Returns:
- This process executor.
-
timeout
Sets a timeout for the process being executed. When this timeout is reached aTimeoutExceptionis thrown and the process is destroyed. This only applies toexecutemethods notstartmethods.- Parameters:
timeout- timeout for running a process.unit- the time unit of the timeout- Returns:
- This process executor.
-
stopper
Sets the helper for stopping the process in case of timeout or cancellation.By default
DestroyProcessStopperis used which just invokesProcess.destroy().- Parameters:
stopper- helper for stopping the process (nullmeansNopProcessStopper- process is not stopped).- Returns:
- This process executor.
-
streams
- Returns:
- current stream handler for the process being executed.
-
streams
Sets a stream handler for the process being executed. This will overwrite any stream redirection that was previously set to use the provided handler.- Parameters:
streams- the stream handler- Returns:
- This process executor.
-
closeTimeout
Sets a timeout for closing standard streams of the process being executed. When this timeout is reached we log a warning but consider that the process has finished. We also flush the streams so that all output read so far is available.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.- Parameters:
timeout- timeout for closing streams of a process.unit- the time unit of the timeout- Returns:
- This process executor.
-
redirectInput
Sets the input stream to redirect to the process' input stream. If this method is invoked multiple times each call overwrites the previous.- Parameters:
input- input stream that will be written to the process input stream (nullmeans nothing will be written to the process input stream).- Returns:
- This process executor.
-
redirectOutput
Redirects the process' output stream to given output stream. If this method is invoked multiple times each call overwrites the previous. UseredirectOutputAlsoTo(OutputStream)if you want to redirect the output to multiple streams.- Parameters:
output- output stream where the process output is redirected to (nullmeansNullOutputStreamwhich acts like a/dev/null).- Returns:
- This process executor.
-
redirectError
Redirects the process' error stream to given output stream. If this method is invoked multiple times each call overwrites the previous. UseredirectErrorAlsoTo(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.
- Parameters:
output- output stream where the process error is redirected to (nullmeansNullOutputStreamwhich acts like a/dev/null).- Returns:
- This process executor.
-
redirectOutputAlsoTo
Redirects the process' output stream also to a given output stream. This method can be used to redirect output to multiple streams.- Parameters:
output- the stream to redirect this output to- Returns:
- This process executor.
-
redirectErrorAlsoTo
Redirects the process' error stream also to a given output stream. This method can be used to redirect error to multiple streams.Calling this method automatically disables merging the process error stream to its output stream.
- Parameters:
output- the output stream to redirect the error stream to- Returns:
- This process executor.
-
pumps
- Returns:
- current PumpStreamHandler (maybe
null). - Throws:
IllegalStateException- if the current stream handler is not an instance ofPumpStreamHandler.- See Also:
-
readOutput
Sets this process executor'sreadOutputproperty.If this property is
true, the process output should be read to a buffer and returned byProcessResult.output(). The initial value isfalse.- Parameters:
readOutput- The new property value- Returns:
- This process executor.
-
info
Deprecated.Logs the process' output to a givenLoggerwithinfolevel.- Parameters:
log- the logger to process the output to- Returns:
- This process executor.
-
debug
Deprecated.Logs the process' output to a givenLoggerwithdebuglevel.- Parameters:
log- the logger to process the output to- Returns:
- This process executor.
-
info
Deprecated.Logs the process' output to aLoggerwith given name usinginfolevel.- Parameters:
name- the name of the logger to process the output to- Returns:
- This process executor.
-
debug
Deprecated.Logs the process' output to aLoggerwith given name usingdebuglevel.- Parameters:
name- the name of the logger to process the output to- Returns:
- This process executor.
-
info
Deprecated.Logs the process' output to aLoggerof the caller class usinginfolevel.- Returns:
- This process executor.
-
debug
Deprecated.Logs the process' output to aLoggerof the caller class usingdebuglevel.- Returns:
- This process executor.
-
redirectOutputAsInfo
Deprecated.Logs the process' output to a givenLoggerwithinfolevel.- Parameters:
log- the logger to output the message to- Returns:
- This process executor.
-
redirectOutputAsDebug
Deprecated.Logs the process' output to a givenLoggerwithdebuglevel.- Parameters:
log- the logger to output the message to- Returns:
- This process executor.
-
redirectOutputAsInfo
Deprecated.Logs the process' output to aLoggerwith given name usinginfolevel.- Parameters:
name- the name of the logger to log to- Returns:
- This process executor.
-
redirectOutputAsDebug
Deprecated.Logs the process' output to aLoggerwith given name usingdebuglevel.- Parameters:
name- the name of the logger to process output to- Returns:
- This process executor.
-
redirectOutputAsInfo
Deprecated.Logs the process' output to aLoggerof the caller class usinginfolevel.- Returns:
- This process executor.
-
redirectOutputAsDebug
Deprecated.Logs the process' output to aLoggerof the caller class usingdebuglevel.- Returns:
- This process executor.
-
redirectErrorAsInfo
Deprecated.Logs the process' error to a givenLoggerwithinfolevel.- Parameters:
log- the logger to process output to- Returns:
- This process executor.
-
redirectErrorAsDebug
Deprecated.Logs the process' error to a givenLoggerwithdebuglevel.- Parameters:
log- the logger to process the error to- Returns:
- This process executor.
-
redirectErrorAsInfo
Deprecated.Logs the process' error to aLoggerwith given name usinginfolevel.- Parameters:
name- the name of the logger to process the error to- Returns:
- This process executor.
-
redirectErrorAsDebug
Deprecated.Logs the process' error to aLoggerwith given name usingdebuglevel.- Parameters:
name- the name of the logger to process the error to- Returns:
- This process executor.
-
redirectErrorAsInfo
Deprecated.Logs the process' error to aLoggerof the caller class usinginfolevel.- Returns:
- This process executor.
-
redirectErrorAsDebug
Deprecated.Logs the process' error to aLoggerof the caller class usingdebuglevel.- Returns:
- This process executor.
-
addDestroyer
Adds a process destroyer to be notified when the process starts and stops.- Parameters:
destroyer- helper for destroying all processes on certain event such as VM exit (notnull).- Returns:
- This process executor.
-
destroyer
Sets the process destroyer to be notified when the process starts and stops.This methods always removes any other
ProcessDestroyerregistered. UseaddDestroyer(ProcessDestroyer)to keep the existing ones.- Parameters:
destroyer- helper for destroying all processes on certain event such as VM exit (maybenull).- Returns:
- This process executor.
-
destroyOnExit
Sets the started process to be destroyed on VM exit (shutdown hooks are executed). If this VM gets killed the started process may not get destroyed.To undo this command call
destroyer(null).- Returns:
- This process executor.
-
listener
Unregister all existing process event handlers and register new one.- Parameters:
listener- process event handler to be set (maybenull).- Returns:
- This process executor.
-
addListener
Register new process event handler.- Parameters:
listener- process event handler to be added.- Returns:
- This process executor.
-
removeListener
Unregister existing process event handler.- Parameters:
listener- process event handler to be removed.- Returns:
- This process executor.
-
removeListeners
Unregister existing process event handlers of given type or its sub-types.- Parameters:
listenerType- process event handler type.- Returns:
- This process executor.
-
clearListeners
Unregister all existing process event handlers.- Returns:
- This process executor.
-
setMessageLogger
Changes how most common messages about starting and waiting for processes are actually logged. By defaultMessageLoggers.DEBUGis used. However if someone is executing a process every secondMessageLoggers.TRACEmay be used e.g.- Parameters:
messageLogger- message logger for certain level.- Returns:
- This process executor.
-
execute
public ProcessResult execute() throws IOException, InterruptedException, TimeoutException, InvalidExitValueExceptionExecutes the sub process. This method waits until the process exits, a timeout occurs or the caller thread gets interrupted. In the latter cases the process gets destroyed as well.- Returns:
- exit code of the finished process.
- Throws:
IOException- an error occurred when process was started or stopped.InterruptedException- this thread was interrupted.TimeoutException- timeout set bytimeout(long, TimeUnit)was reached.InvalidExitValueException- if invalid exit value was returned (@seeexitValues(Integer...)).
-
executeNoTimeout
public ProcessResult executeNoTimeout() throws IOException, InterruptedException, InvalidExitValueExceptionExecutes the sub process. This method waits until the process exits. Value passed totimeout(long, TimeUnit)is ignored (useexecute()for timeout).- Returns:
- exit code of the finished process.
- Throws:
IOException- an error occurred when process was started or stopped.InterruptedException- this thread was interrupted.InvalidExitValueException- if invalid exit value was returned (@seeexitValues(Integer...)).
-
checkExitValue
Check the exit value of given process result. This can be used by unit tests.- Parameters:
result- process result which maybe constructed by a unit test.- Throws:
InvalidExitValueException- if the given exit value was rejected.
-
start
Start the sub process. This method does not wait until the process exits. Value passed totimeout(long, TimeUnit)is ignored. UseFuture.get()to wait for the process to finish. Invokefuture.cancel(true);to destroy the process.- Returns:
- Future representing the exit value of the finished process.
- Throws:
IOException- an error occurred when process was started.
-
startInternal
Start the process and its stream handlers.- Returns:
- process the started process.
- Throws:
IOException- the process or its stream handlers couldn't start (in the latter case we also destroy the process).
-
newExecutor
-
invokeSubmit
Override this to customize how the waiting task is started in the background.- Type Parameters:
T- the type of the task- Parameters:
executor- the executor service to submit the task ontask- the task to be submitted- Returns:
- the future of the task
-
wrapTask
-
redirectOutput(OutputStream)andSlf4jStream