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) 设置超时时间
相关推荐
qq_124987075330 分钟前
基于SSM的动物保护系统的设计与实现(源码+论文+部署+安装)
java·数据库·spring boot·毕业设计·ssm·计算机毕业设计
Coder_Boy_36 分钟前
基于SpringAI的在线考试系统-考试系统开发流程案例
java·数据库·人工智能·spring boot·后端
Mr_sun.37 分钟前
Day06——权限认证-项目集成
java
瑶山39 分钟前
Spring Cloud微服务搭建四、集成RocketMQ消息队列
java·spring cloud·微服务·rocketmq·dashboard
abluckyboy1 小时前
Java 实现求 n 的 n^n 次方的最后一位数字
java·python·算法
2301_818732061 小时前
前端调用控制层接口,进不去,报错415,类型不匹配
java·spring boot·spring·tomcat·intellij-idea
2501_941982051 小时前
深度对比:Java、Go、Python 实现企微外部群推送,哪个效率更高?
java·golang·企业微信
马猴烧酒.1 小时前
【面试八股|JAVA多线程】JAVA多线程常考面试题详解
java·服务器·数据库
sino爱学习2 小时前
高性能线程池实践:Dubbo EagerThreadPool 设计与应用
java·后端
风生u3 小时前
activiti7 详解
java