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 SummaryNested ClassesModifier and TypeClassDescriptionstatic classDefaultExecutor.Builder<T extends DefaultExecutor.Builder<T>>Constructs a newDefaultExecutor.
- 
Field SummaryFields inherited from interface org.apache.commons.exec.ExecutorINVALID_EXITVALUE
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic DefaultExecutor.Builder<?> builder()Creates a new builder.protected ThreadcreateThread(Runnable runnable, String name) Creates a thread waiting for the result of an asynchronous execution.intexecute(CommandLine command) Executes a command synchronously.intexecute(CommandLine command, Map<String, String> environment) Executes a command synchronously.voidexecute(CommandLine command, Map<String, String> environment, ExecuteResultHandler handler) Executes a command asynchronously.voidexecute(CommandLine command, ExecuteResultHandler handler) Executes a command asynchronously.protected ThreadGets 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.booleanisFailure(int exitValue) Tests whetherexitValuesignals a failure.protected ProcessCreates a process that runs a command.protected ProcessCreates a process that runs a command.voidsetExitValue(int value) Sets theexitValueof the process to be considered successful.voidsetExitValues(int[] values) Sets a list ofexitValueof the process to be considered successful.voidsetProcessDestroyer(ProcessDestroyer processDestroyer) Sets the handler for cleanup of started processes if the main process is going to terminate.voidsetStreamHandler(ExecuteStreamHandler streamHandler) Sets a custom the StreamHandler used for providing input and retrieving the output.voidsetWatchdog(ExecuteWatchdog watchdog) Sets the watchdog used to kill of processes running, typically, too long time.voidsetWorkingDirectory(File workingDirectory) Deprecated.Methods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.commons.exec.ExecutorgetWorkingDirectoryPath
- 
Constructor Details- 
DefaultExecutorDeprecated.Constructs a defaultPumpStreamHandlerand sets the working directory of the subprocess to the current working directory. ThePumpStreamHandlerpumps the output of the subprocess into ourSystem.outandSystem.errto avoid into ourSystem.outandSystem.errto avoid a blocked or deadlocked subprocess (seeProcess).
 
- 
- 
Method Details- 
builderCreates a new builder.- Returns:
- a new builder.
- Since:
- 1.4.0
 
- 
createThreadCreates 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
 
- 
executeDescription copied from interface:ExecutorExecutes a command synchronously. The child process inherits all environment variables of the parent process.- Specified by:
- executein interface- Executor
- 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 failure- Executor.setExitValue(int).
- IOException- If an I/O error occurs.
- See Also:
 
- 
executepublic void execute(CommandLine command, ExecuteResultHandler handler) throws ExecuteException, IOException Description copied from interface:ExecutorExecutes a command asynchronously. The child process inherits all environment variables of the parent process. Result provided to callback handler.- Specified by:
- executein interface- Executor
- 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:
 
- 
executepublic int execute(CommandLine command, Map<String, String> environment) throws ExecuteException, IOExceptionDescription copied from interface:ExecutorExecutes a command synchronously.- Specified by:
- executein interface- Executor
- 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 failure- Executor.setExitValue(int).
- IOException- If an I/O error occurs.
- See Also:
 
- 
executepublic void execute(CommandLine command, Map<String, String> environment, ExecuteResultHandler handler) throws ExecuteException, IOExceptionDescription copied from interface:ExecutorExecutes a command asynchronously. The child process inherits all environment variables of the parent process. Result provided to callback handler.- Specified by:
- executein interface- Executor
- 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:
 
- 
getExecutorThreadGets the worker thread being used for asynchronous execution.- Returns:
- the worker thread.
 
- 
getProcessDestroyerDescription copied from interface:ExecutorSets the handler for cleanup of started processes if the main process is going to terminate.- Specified by:
- getProcessDestroyerin interface- Executor
- Returns:
- the ProcessDestroyer.
- See Also:
 
- 
getStreamHandlerDescription copied from interface:ExecutorGets the StreamHandler used for providing input and retrieving the output.- Specified by:
- getStreamHandlerin interface- Executor
- Returns:
- the StreamHandler.
- See Also:
 
- 
getWatchdogDescription copied from interface:ExecutorGets the watchdog used to kill of processes running, typically, too long time.- Specified by:
- getWatchdogin interface- Executor
- Returns:
- the watchdog.
- See Also:
 
- 
getWorkingDirectoryDescription copied from interface:ExecutorGets the working directory of the created process.- Specified by:
- getWorkingDirectoryin interface- Executor
- Returns:
- the working directory.
- See Also:
 
- 
isFailureDescription copied from interface:ExecutorTests whetherexitValuesignals 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.
- 
launchprotected 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.
 
- 
launchprotected Process launch(CommandLine command, Map<String, String> env, Path 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.
- Since:
- 1.5.0
 
- 
setExitValueDescription copied from interface:ExecutorSets theexitValueof the process to be considered successful. If a different exit value is returned by the process thenExecutor.execute(CommandLine)will throw anExecuteException.- Specified by:
- setExitValuein interface- Executor
- Parameters:
- value- the exit code representing successful execution.
- See Also:
 
- 
setExitValuesDescription copied from interface:ExecutorSets a list ofexitValueof 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:
- setExitValuesin interface- Executor
- Parameters:
- values- a list of the exit codes.
- See Also:
 
- 
setProcessDestroyerDescription copied from interface:ExecutorSets the handler for cleanup of started processes if the main process is going to terminate.- Specified by:
- setProcessDestroyerin interface- Executor
- Parameters:
- processDestroyer- the ProcessDestroyer.
- See Also:
 
- 
setStreamHandlerDescription copied from interface:ExecutorSets 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:
- setStreamHandlerin interface- Executor
- Parameters:
- streamHandler- the stream handler.
- See Also:
 
- 
setWatchdogDescription copied from interface:ExecutorSets the watchdog used to kill of processes running, typically, too long time.- Specified by:
- setWatchdogin interface- Executor
- Parameters:
- watchdog- the watchdog.
- See Also:
 
- 
setWorkingDirectoryDeprecated.Sets the working directory.- Specified by:
- setWorkingDirectoryin interface- Executor
- Parameters:
- workingDirectory- the working directory.
- See Also:
 
 
- 
DefaultExecutor.Builder.get().