commons-exec

概述

用于执行外部命令和进程的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) 设置超时时间
相关推荐
brave_zhao12 小时前
launch4j亲测打包java的jar转为exe执行文件
java
利刃大大12 小时前
【RabbitMQ】SpringBoot整合RabbitMQ:工作队列 && 发布/订阅模式 && 路由模式 && 通配符模式
java·spring boot·消息队列·rabbitmq·java-rabbitmq
lkbhua莱克瓦2412 小时前
进阶-存储对象1-视图
java·数据库·sql·mysql·视图
yangminlei12 小时前
Spring Boot 自动配置原理与自定义 Starter 开发实战
java·数据库·spring boot
悟空码字12 小时前
10分钟搞定!SpringBoot集成腾讯云短信全攻略,从配置到发送一气呵成
java·spring boot·后端
爱编程的小吴12 小时前
【力扣练习题】151. 反转字符串中的单词
java·算法·leetcode
未来龙皇小蓝12 小时前
Spring注入Bean流程及其理解
java·spring boot·后端·spring·代理模式
知秋正在99612 小时前
Java实现Html保存为.mhtml文件
java·开发语言·html
码头整点薯条12 小时前
大数据量查询处理方案
java
菜鸟233号12 小时前
力扣474 一和零 java实现
java·数据结构·算法·leetcode·动态规划