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. |
| 限定符和类型 | 方法和说明 |
|---|---|
int |
execute(boolean async,
CommandLine command) |
int |
execute(boolean async,
CommandLine command,
ExecuteResultHandler handler) |
int |
execute(boolean async,
CommandLine command,
Map<String,String> environment) |
int |
execute(boolean async,
CommandLine command,
Map<String,String> environment,
File workingDir,
ExecuteStreamHandler executeStreamHandler,
ExecuteResultHandler resultHandler) |
int |
execute(CommandLine command)
Methods for starting synchronous execution.
|
int |
execute(CommandLine command,
ExecuteResultHandler handler)
Methods for starting asynchronous execution.
|
int |
execute(CommandLine command,
Map<String,String> environment)
Methods for starting synchronous execution.
|
int |
execute(CommandLine command,
Map<String,String> environment,
File workingDirectory,
ExecuteStreamHandler streamHandler,
ExecuteResultHandler handler)
Methods for starting asynchronous execution.
|
protected int |
executeInternal(CommandLine command,
Map<String,String> environment,
File dir,
ExecuteStreamHandler streamHandler)
Execute an internal process.
|
ExecutorService |
getExecutorService() |
InstructionSequenceDestroyer |
getProcessDestroyer()
Set the handler for cleanup of started processes if the main process
is going to terminate.
|
ExecuteResultHandler |
getResultHandler() |
ExecuteStreamHandler |
getStreamHandler() |
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 InstructionSequence |
launch(CommandLine command,
Map<String,String> env,
File workingDir)
Creates a process that runs a command.
|
void |
setExecutorService(ExecutorService executorService) |
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 |
setLauncher(CommandLauncher launcher) |
void |
setProcessDestroyer(InstructionSequenceDestroyer processDestroyer)
Get the handler for cleanup of started processes if the main process
is going to terminate.
|
void |
setResultHandler(ExecuteResultHandler resultHandler) |
void |
setStreamHandler(ExecuteStreamHandler streamHandler) |
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()
getStreamHandler 在接口中 CommandLineExecutorpublic void setStreamHandler(ExecuteStreamHandler streamHandler)
setStreamHandler 在接口中 CommandLineExecutorpublic ExecuteResultHandler getResultHandler()
getResultHandler 在接口中 CommandLineExecutorpublic void setResultHandler(ExecuteResultHandler resultHandler)
setResultHandler 在接口中 CommandLineExecutorpublic ExecutorService getExecutorService()
public void setExecutorService(ExecutorService executorService)
public void setLauncher(CommandLauncher launcher)
public ExecuteWatchdog getWatchdog()
CommandLineExecutorgetWatchdog 在接口中 CommandLineExecutorCommandLineExecutor.getWatchdog()public void setWatchdog(ExecuteWatchdog watchDog)
CommandLineExecutorsetWatchdog 在接口中 CommandLineExecutorwatchDog - the watchdogCommandLineExecutor.setWatchdog(com.jn.langx.commandline.ExecuteWatchdog)public InstructionSequenceDestroyer getProcessDestroyer()
CommandLineExecutorgetProcessDestroyer 在接口中 CommandLineExecutorCommandLineExecutor.getProcessDestroyer()public void setProcessDestroyer(InstructionSequenceDestroyer processDestroyer)
CommandLineExecutorsetProcessDestroyer 在接口中 CommandLineExecutorprocessDestroyer - the ProcessDestroyerCommandLineExecutor.setProcessDestroyer(InstructionSequenceDestroyer)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).IOExceptionpublic int execute(boolean async,
CommandLine command)
throws IOException
execute 在接口中 CommandLineExecutorIOExceptionpublic 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).IOExceptionpublic int execute(boolean async,
CommandLine command,
Map<String,String> environment)
throws IOException
execute 在接口中 CommandLineExecutorIOExceptionpublic int execute(CommandLine command, ExecuteResultHandler handler) throws ExecuteException, IOException
CommandLineExecutorexecute 在接口中 CommandLineExecutorcommand - the command to executehandler - capture process termination and exit codeExecuteException - execution of subprocess failedIOExceptionpublic int execute(boolean async,
CommandLine command,
ExecuteResultHandler handler)
throws IOException
execute 在接口中 CommandLineExecutorIOExceptionpublic int execute(CommandLine command, Map<String,String> environment, File workingDirectory, ExecuteStreamHandler streamHandler, 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 failedIOExceptionpublic int execute(boolean async,
CommandLine command,
Map<String,String> environment,
File workingDir,
ExecuteStreamHandler executeStreamHandler,
ExecuteResultHandler resultHandler)
throws IOException
execute 在接口中 CommandLineExecutorIOExceptionpublic void setExitValue(int value)
CommandLineExecutorexitValue of the process to be considered
successful.setExitValue 在接口中 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)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 InstructionSequence launch(CommandLine command, Map<String,String> env, File workingDir) throws IOException
command - the command to runenv - the environment for the commandworkingDir - the working directory for the commandIOException - forwarded from the particular launcher usedprotected int executeInternal(CommandLine command, Map<String,String> environment, File dir, ExecuteStreamHandler streamHandler) throws IOException
command - the command to executeenvironment - the execution environmentdir - the working directorystreamHandler - process the streams (in, out, err) of the processIOException - executing the process failedCopyright © 2022. All rights reserved.