Package org.apache.commons.exec
Class DefaultExecutor
java.lang.Object
org.apache.commons.exec.DefaultExecutor
- All Implemented Interfaces:
Executor
- Direct Known Subclasses:
DaemonExecutor
The default class to start a subprocess. The implementation allows to
- set a current working directory for the subprocess
- provide a set of environment variables passed to the subprocess
- capture the subprocess output of stdout and stderr using an ExecuteStreamHandler
- kill long-running processes using an ExecuteWatchdog
- define a set of expected exit values
- terminate any started processes when the main process is terminating using a ProcessDestroyer
Executor exec = DefaultExecutor.builder().get(); CommandLine cl = new CommandLine("ls -l"); int exitvalue = exec.execute(cl);
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
DefaultExecutor.Builder<T extends DefaultExecutor.Builder<T>>
Constructs a new builder. -
Field Summary
Fields inherited from interface org.apache.commons.exec.Executor
INVALID_EXITVALUE
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic DefaultExecutor.Builder
<?> builder()
Creates a new builder.protected Thread
createThread
(Runnable runnable, String name) Creates a thread waiting for the result of an asynchronous execution.int
execute
(CommandLine command) Executes a command synchronously.int
execute
(CommandLine command, Map<String, String> environment) Executes a command synchronously.void
execute
(CommandLine command, Map<String, String> environment, ExecuteResultHandler handler) Executes a command asynchronously.void
execute
(CommandLine command, ExecuteResultHandler handler) Executes a command asynchronously.protected Thread
Gets the worker thread being used for asynchronous execution.Sets the handler for cleanup of started processes if the main process is going to terminate.Gets the StreamHandler used for providing input and retrieving the output.Gets the watchdog used to kill of processes running, typically, too long time.Gets the working directory of the created process.boolean
isFailure
(int exitValue) Tests whetherexitValue
signals a failure.protected Process
Creates a process that runs a command.void
setExitValue
(int value) Sets theexitValue
of the process to be considered successful.void
setExitValues
(int[] values) Sets a list ofexitValue
of the process to be considered successful.void
setProcessDestroyer
(ProcessDestroyer processDestroyer) Sets the handler for cleanup of started processes if the main process is going to terminate.void
setStreamHandler
(ExecuteStreamHandler streamHandler) Sets a custom the StreamHandler used for providing input and retrieving the output.void
setWatchdog
(ExecuteWatchdog watchdog) Sets the watchdog used to kill of processes running, typically, too long time.void
setWorkingDirectory
(File workingDirectory) Deprecated.
-
Constructor Details
-
DefaultExecutor
Deprecated.Constructs a defaultPumpStreamHandler
and sets the working directory of the subprocess to the current working directory. ThePumpStreamHandler
pumps the output of the subprocess into ourSystem.out
andSystem.err
to avoid into ourSystem.out
andSystem.err
to avoid a blocked or deadlocked subprocess (seeProcess
).
-
-
Method Details
-
builder
Creates a new builder.- Returns:
- a new builder.
- Since:
- 1.4.0
-
createThread
Creates a thread waiting for the result of an asynchronous execution.- Parameters:
runnable
- the runnable passed to the thread.name
- the name of the thread.- Returns:
- the thread
-
execute
Description copied from interface:Executor
Executes a command synchronously. The child process inherits all environment variables of the parent process.- Specified by:
execute
in interfaceExecutor
- Parameters:
command
- the command to execute.- Returns:
- process exit value.
- Throws:
ExecuteException
- execution of subprocess failed or the subprocess returned a exit value indicating a failureExecutor.setExitValue(int)
.IOException
- If an I/O error occurs.- See Also:
-
execute
public void execute(CommandLine command, ExecuteResultHandler handler) throws ExecuteException, IOException Description copied from interface:Executor
Executes a command asynchronously. The child process inherits all environment variables of the parent process. Result provided to callback handler.- Specified by:
execute
in interfaceExecutor
- Parameters:
command
- the command to execute.handler
- capture process termination and exit code.- Throws:
ExecuteException
- execution of subprocess failed.IOException
- If an I/O error occurs.- See Also:
-
execute
public int execute(CommandLine command, Map<String, String> environment) throws ExecuteException, IOExceptionDescription copied from interface:Executor
Executes a command synchronously.- Specified by:
execute
in interfaceExecutor
- Parameters:
command
- the command to execute.environment
- The environment for the new process. If null, the environment of the current process is used.- Returns:
- process exit value.
- Throws:
ExecuteException
- execution of subprocess failed or the subprocess returned a exit value indicating a failureExecutor.setExitValue(int)
.IOException
- If an I/O error occurs.- See Also:
-
execute
public void execute(CommandLine command, Map<String, String> environment, ExecuteResultHandler handler) throws ExecuteException, IOExceptionDescription copied from interface:Executor
Executes a command asynchronously. The child process inherits all environment variables of the parent process. Result provided to callback handler.- Specified by:
execute
in interfaceExecutor
- Parameters:
command
- the command to execute.environment
- The environment for the new process. If null, the environment of the current process is used.handler
- capture process termination and exit code.- Throws:
ExecuteException
- execution of subprocess failed.IOException
- If an I/O error occurs.- See Also:
-
getExecutorThread
Gets the worker thread being used for asynchronous execution.- Returns:
- the worker thread.
-
getProcessDestroyer
Description copied from interface:Executor
Sets the handler for cleanup of started processes if the main process is going to terminate.- Specified by:
getProcessDestroyer
in interfaceExecutor
- Returns:
- the ProcessDestroyer.
- See Also:
-
getStreamHandler
Description copied from interface:Executor
Gets the StreamHandler used for providing input and retrieving the output.- Specified by:
getStreamHandler
in interfaceExecutor
- Returns:
- the StreamHandler.
- See Also:
-
getWatchdog
Description copied from interface:Executor
Gets the watchdog used to kill of processes running, typically, too long time.- Specified by:
getWatchdog
in interfaceExecutor
- Returns:
- the watchdog.
- See Also:
-
getWorkingDirectory
Description copied from interface:Executor
Gets the working directory of the created process.- Specified by:
getWorkingDirectory
in interfaceExecutor
- Returns:
- the working directory.
- See Also:
-
isFailure
Description copied from interface:Executor
Tests whetherexitValue
signals a failure. If no exit values are set than the default conventions of the OS is used. e.g. most OS regard an exit code of '0' as successful execution and everything else as failure. -
launch
protected Process launch(CommandLine command, Map<String, String> env, File workingDirectory) throws IOExceptionCreates a process that runs a command.- Parameters:
command
- the command to run.env
- the environment for the command.workingDirectory
- the working directory for the command.- Returns:
- the process started.
- Throws:
IOException
- forwarded from the particular launcher used.
-
setExitValue
Description copied from interface:Executor
Sets theexitValue
of the process to be considered successful. If a different exit value is returned by the process thenExecutor.execute(CommandLine)
will throw anExecuteException
.- Specified by:
setExitValue
in interfaceExecutor
- Parameters:
value
- the exit code representing successful execution.- See Also:
-
setExitValues
Description copied from interface:Executor
Sets a list ofexitValue
of the process to be considered successful. The caller can pass one of the following values.- an array of exit values to be considered successful
- an empty array for auto-detect of successful exit codes relying on
Executor.isFailure(int)
- null to indicate to skip checking of exit codes
Executor.execute(CommandLine)
will throw anExecuteException
.- Specified by:
setExitValues
in interfaceExecutor
- Parameters:
values
- a list of the exit codes.- See Also:
-
setProcessDestroyer
Description copied from interface:Executor
Sets the handler for cleanup of started processes if the main process is going to terminate.- Specified by:
setProcessDestroyer
in interfaceExecutor
- Parameters:
processDestroyer
- the ProcessDestroyer.- See Also:
-
setStreamHandler
Description copied from interface:Executor
Sets a custom the StreamHandler used for providing input and retrieving the output. If you don't provide a proper stream handler the executed process might block when writing to stdout and/or stderr (seeProcess
).- Specified by:
setStreamHandler
in interfaceExecutor
- Parameters:
streamHandler
- the stream handler.- See Also:
-
setWatchdog
Description copied from interface:Executor
Sets the watchdog used to kill of processes running, typically, too long time.- Specified by:
setWatchdog
in interfaceExecutor
- Parameters:
watchdog
- the watchdog.- See Also:
-
setWorkingDirectory
Deprecated.Sets the working directory.- Specified by:
setWorkingDirectory
in interfaceExecutor
- Parameters:
workingDirectory
- the working directory.- See Also:
-
DefaultExecutor.Builder.get()
.