概述
用于执行外部命令和进程的Java库。它提供了一种简单而灵活的方式来启动、监控和终止外部进程,并处理它们的输入和输出。
maven依赖
xml
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-exec -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-exec</artifactId>
<version>1.5.0</version>
</dependency>
示例
java
String command = "ping localhost";
ByteArrayOutputStream susStream = new ByteArrayOutputStream();//接收正常结果流
ByteArrayOutputStream errStream = new ByteArrayOutputStream(); //接收异常结果流
CommandLine commandLine = CommandLine.parse(command);
DefaultExecutor exec = new DefaultExecutor();
PumpStreamHandler streamHandler = new PumpStreamHandler(susStream, errStream);
exec.setStreamHandler(streamHandler);
int code = exec.execute(commandLine);
System.out.println("退出代码: " + code);
System.out.println(susStream.toString("GBK"));
System.out.println(errStream.toString("GBK"));
常用API
CommandLine 命令行对象
方法 | 描述 |
---|---|
parse(String line) parse(String line, Map<String, ?> substitutionMap) | 静态方法,解析命令行 |
CommandLine(CommandLine other) CommandLine(File executable) CommandLine(Path executable) CommandLine(String executable) | 构造方法 |
addArgument(String argument) addArgument(String argument, boolean handleQuoting) | 添加一个参数 |
addArguments(String addArguments) addArguments(String addArguments, boolean handleQuoting) addArguments(String[] addArguments) addArguments(String[] addArguments, boolean handleQuoting) | 添加多个参数 |
getArguments() | 获取参数 |
getExecutable() | 获取可执行的命令字符串 |
isFile() | 判断是否是可执行文件 |
getSubstitutionMap() setSubstitutionMap(Map<String, ?> substitutionMap) | 设置替换映射,用于替换参数中的变量:${变量} |
执行器
- DefaultExecutor
方法 | 描述 |
---|---|
builder() | 静态方法,获取构造器 |
execute(CommandLine command) execute(CommandLine command, ExecuteResultHandler handler) execute(CommandLine command, Map<String, String> environment) execute(CommandLine command, Map<String, String> environment, ExecuteResultHandler handler) | 执行命令行 |
getProcessDestroyer() setProcessDestroyer(ProcessDestroyer processDestroyer) | 获取或设置进程清理操作器 |
getStreamHandler() setStreamHandler(ExecuteStreamHandler streamHandler) | 获取或设置执行结果流处理器 |
getWatchdog() setWatchdog(ExecuteWatchdog watchdog) | 获取或设置执行监控对象 |
getWorkingDirectory() | 获取工作目录 |
isFailure(int exitValue) | 通过执行结果码判断命令行是否执行成功 |
setExitValue(int value) | 设置进程退出值 |
setExitValues(int[] values) | 设置进程退出值 |
- DefaultExecutor.Builder
方法 | 描述 |
---|---|
get() | 获取DefaultExecutor对象 |
setExecuteStreamHandler(ExecuteStreamHandler executeStreamHandler) | 设置执行结果流处理器 |
setThreadFactory(ThreadFactory threadFactory) | 设置线程工厂 |
setWorkingDirectory(File workingDirectory) setWorkingDirectory(Path workingDirectory) | 设置工作目录 |
- DaemonExecutor DefaultExecutor的子类,用于异步执行,方法同DefaultExecutor一致
结果流处理
- PumpStreamHandler
方法 | 描述 |
---|---|
PumpStreamHandler() PumpStreamHandler(OutputStream allOutputStream) PumpStreamHandler(OutputStream outputStream, OutputStream errorOutputStream) PumpStreamHandler(OutputStream outputStream, OutputStream errorOutputStream, InputStream inputStream) | 构造方法 |
setProcessErrorStream(InputStream is) | 进程错误流 |
setProcessInputStream(OutputStream os) | 进程输出流 |
setProcessOutputStream(InputStream is) | 进程输入流 |
setStopTimeout(Duration timeout) | 超时时间 |
start() | 开始 |
stop() | 结束 |
监控对象
- Watchdog
方法 | 描述 |
---|---|
builder() | 获取构造器 |
addTimeoutObserver(TimeoutObserver to) | |
removeTimeoutObserver(TimeoutObserver to) | |
run() | |
start() | |
stop() |
- Watchdog.Builder
方法 | 描述 |
---|---|
get() | 获取Watchdog对象 |
setThreadFactory(ThreadFactory threadFactory) | 设置线程工厂 |
setTimeout(Duration timeout) | 设置超时时间 |