public class DefaultCommandLineExecutor extends Object implements CommandLineExecutor
The following example shows the basic usage:
Executor exec = new DefaultExecutor();
CommandLine cl = new CommandLine("ls -l");
int exitvalue = exec.execute(cl);
INVALID_EXITVALUE| 构造器和说明 |
|---|
DefaultCommandLineExecutor()
Default constructor creating a default
PumpStreamHandler
and sets the working directory of the subprocess to the current
working directory. |
| 限定符和类型 | 方法和说明 |
|---|---|
protected Thread |
createThread(Runnable runnable,
String name)
Factory method to create a thread waiting for the result of an
asynchronous execution.
|
int |
execute(CommandLine command)
Methods for starting synchronous execution.
|
void |
execute(CommandLine command,
ExecuteResultHandler handler)
Methods for starting asynchronous execution.
|
int |
execute(CommandLine command,
Map<String,String> environment)
Methods for starting synchronous execution.
|
void |
execute(CommandLine command,
Map<String,String> environment,
ExecuteResultHandler handler)
Methods for starting asynchronous execution.
|
protected Thread |
getExecutorThread()
Get the worker thread being used for asynchronous execution.
|
ProcessDestroyer |
getProcessDestroyer()
Set the handler for cleanup of started processes if the main process
is going to terminate.
|
ExecuteStreamHandler |
getStreamHandler()
Get the StreamHandler used for providing input and
retrieving the output.
|
ExecuteWatchdog |
getWatchdog()
Get the watchdog used to kill of processes running,
typically, too long time.
|
File |
getWorkingDirectory()
Get the working directory of the created process.
|
boolean |
isFailure(int exitValue)
Checks whether
exitValue signals a failure. |
protected Process |
launch(CommandLine command,
Map<String,String> env,
File dir)
Creates a process that runs a command.
|
void |
setExitValue(int value)
Define the
exitValue of the process to be considered
successful. |
void |
setExitValues(int[] values)
Define a list of
exitValue of the process to be considered
successful. |
void |
setProcessDestroyer(ProcessDestroyer processDestroyer)
Get the handler for cleanup of started processes if the main process
is going to terminate.
|
void |
setStreamHandler(ExecuteStreamHandler streamHandler)
Set a custom the StreamHandler used for providing
input and retrieving the output.
|
void |
setWatchdog(ExecuteWatchdog watchDog)
Set the watchdog used to kill of processes running,
typically, too long time.
|
void |
setWorkingDirectory(File dir)
Set the working directory of the created process.
|
public DefaultCommandLineExecutor()
PumpStreamHandler
and sets the working directory of the subprocess to the current
working directory.
The PumpStreamHandler pumps the output of the subprocess
into our System.out and System.err to avoid
into our System.out and System.err to avoid
a blocked or deadlocked subprocess (seeProcess).
public ExecuteStreamHandler getStreamHandler()
CommandLineExecutorgetStreamHandler 在接口中 CommandLineExecutorCommandLineExecutor.getStreamHandler()public void setStreamHandler(ExecuteStreamHandler streamHandler)
CommandLineExecutorProcess).setStreamHandler 在接口中 CommandLineExecutorstreamHandler - the stream handlerCommandLineExecutor.setStreamHandler(com.jn.langx.commandline.ExecuteStreamHandler)public ExecuteWatchdog getWatchdog()
CommandLineExecutorgetWatchdog 在接口中 CommandLineExecutorCommandLineExecutor.getWatchdog()public void setWatchdog(ExecuteWatchdog watchDog)
CommandLineExecutorsetWatchdog 在接口中 CommandLineExecutorwatchDog - the watchdogCommandLineExecutor.setWatchdog(com.jn.langx.commandline.ExecuteWatchdog)public ProcessDestroyer getProcessDestroyer()
CommandLineExecutorgetProcessDestroyer 在接口中 CommandLineExecutorCommandLineExecutor.getProcessDestroyer()public void setProcessDestroyer(ProcessDestroyer processDestroyer)
CommandLineExecutorsetProcessDestroyer 在接口中 CommandLineExecutorprocessDestroyer - the ProcessDestroyerCommandLineExecutor.setProcessDestroyer(ProcessDestroyer)public File getWorkingDirectory()
CommandLineExecutorgetWorkingDirectory 在接口中 CommandLineExecutorCommandLineExecutor.getWorkingDirectory()public void setWorkingDirectory(File dir)
CommandLineExecutorsetWorkingDirectory 在接口中 CommandLineExecutordir - the working directoryCommandLineExecutor.setWorkingDirectory(java.io.File)public int execute(CommandLine command) throws ExecuteException, IOException
CommandLineExecutorexecute 在接口中 CommandLineExecutorcommand - the command to executeExecuteException - execution of subprocess failed or the
subprocess returned a exit value indicating a failure
CommandLineExecutor.setExitValue(int).IOExceptionCommandLineExecutor.execute(CommandLine)public int execute(CommandLine command, Map<String,String> environment) throws ExecuteException, IOException
CommandLineExecutorexecute 在接口中 CommandLineExecutorcommand - the command to executeenvironment - The environment for the new process. If null, the
environment of the current process is used.ExecuteException - execution of subprocess failed or the
subprocess returned a exit value indicating a failure
CommandLineExecutor.setExitValue(int).IOExceptionCommandLineExecutor.execute(CommandLine, java.util.Map)public void execute(CommandLine command, ExecuteResultHandler handler) throws ExecuteException, IOException
CommandLineExecutorexecute 在接口中 CommandLineExecutorcommand - the command to executehandler - capture process termination and exit codeExecuteException - execution of subprocess failedIOExceptionCommandLineExecutor.execute(CommandLine,
com.jn.langx.commandline.ExecuteResultHandler)public void execute(CommandLine command, Map<String,String> environment, ExecuteResultHandler handler) throws ExecuteException, IOException
CommandLineExecutorexecute 在接口中 CommandLineExecutorcommand - the command to executeenvironment - The environment for the new process. If null, the
environment of the current process is used.handler - capture process termination and exit codeExecuteException - execution of subprocess failedIOExceptionCommandLineExecutor.execute(CommandLine,
java.util.Map, com.jn.langx.commandline.ExecuteResultHandler)public void setExitValue(int value)
CommandLineExecutorexitValue of the process to be considered
successful. If a different exit value is returned by
the process then CommandLineExecutor.execute(CommandLine)
will throw an ExecuteExceptionsetExitValue 在接口中 CommandLineExecutorvalue - the exit code representing successful executionCommandLineExecutor.setExitValue(int)public void setExitValues(int[] values)
CommandLineExecutorexitValue of the process to be considered
successful. The caller can pass one of the following values
CommandLineExecutor.isFailure(int)CommandLineExecutor.execute(CommandLine) will
throw an ExecuteException.setExitValues 在接口中 CommandLineExecutorvalues - a list of the exit codesCommandLineExecutor.setExitValues(int[])public boolean isFailure(int exitValue)
CommandLineExecutorexitValue 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.isFailure 在接口中 CommandLineExecutorexitValue - the exit value (return code) to be checkedtrue if exitValue signals a failureCommandLineExecutor.isFailure(int)protected Thread createThread(Runnable runnable, String name)
runnable - the runnable passed to the threadname - the name of the threadprotected Process launch(CommandLine command, Map<String,String> env, File dir) throws IOException
command - the command to runenv - the environment for the commanddir - the working directory for the commandIOException - forwarded from the particular launcher usedprotected Thread getExecutorThread()
Copyright © 2020. All rights reserved.