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) 设置超时时间
相关推荐
z1234567898612 分钟前
2026最新两款AI编程工具深度对比实测
java·数据库·ai编程
yaoxin5211231 小时前
470. Java 反射 - Member 接口与 AccessFlag
java·开发语言·python
做个文艺程序员1 小时前
Linux第24篇:Java应用监控体系搭建:Prometheus+Grafana可视化运维
java·grafana·prometheus
小钻风33662 小时前
Spring Boot 文件上传详解:深入理解 MultipartFile 的使用与原理
java·开发语言
其美杰布-富贵-李2 小时前
Spring Boot 工程开发全流程说明
java·spring boot·后端
前端双越老师3 小时前
如何以前端视角(非0基础)学 Java ?
java·node.js·全栈
dear_bi_MyOnly4 小时前
【SpringBoot配置文件】
java·spring boot·后端·学习·spring·java-ee·学习方法
做个文艺程序员4 小时前
Linux第22篇:用Docker容器化你的Java SaaS应用:一次构建,随处运行
java·docker·容器
其美杰布-富贵-李5 小时前
Spring Boot 依赖注入说明文档
java·spring boot·python
tachibana25 小时前
hot100 数组中的第K个最大元素(215)
java·数据结构·算法·leetcode