使用getRuntime().exec()时判断程序是否“正常”结束的方法

Java调用外部程序判断状态
本文介绍如何使用Java调用外部程序,并通过检查exitValue()来判断调用的程序是否成功执行。针对cognos8刷新cube命令的场景,提供了一个具体的示例。
在用java.lang.Runtime.getRuntime().exec()调用其他程序时,比如像指向cognos8刷新cube的命令:
java.lang.Process process = java.lang.Runtime.getRuntime().exec(cogtr -n2 -nologo -r4 -c -fD:/cog/mdl8/xmlconf/单位客户余额日统计.xml -mD:/cog/mdl8/单位客户余额日统计.mdl);
cogtr在执行完以后在命令行中是没有返回信息的,如果执行出错,也只能在cognos日志中看到错误信息。查看java.lang.Process的api信息,没发现好的方法获取cogtr出错时的返回信息。

目前判断exec()所调用的程序是否正常执行完毕的方法是:
java.lang.Process process = java.lang.Runtime.getRuntime().exec(cogtr -n2 -nologo -r4 -c -fD:/cog/mdl8/xmlconf/单位客户余额日统计.xml -mD:/cog/mdl8/单位客户余额日统计.mdl);
if(process.exitValue() == 0){
System.out.println("OK");
}else{
System.out.println("ERROR");
}
package com.example.yingdaotools.yingdao.controller; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.concurrent.TimeUnit; public class LanyinRPAExecutor { public static void main(String[] args) { // 配置蓝印RPA执行路径和RPA文件路径 String rpaExePath = "F:\\lyin\\LanYin_RPA\\lyrpa.exe"; String rpaFilePath = "F:\\lyin\\lyin\\在百度里模拟自动输入并点击搜索按钮.rpa"; try { // 构建执行命令(处理路径中的空格) String command = String.format( "\"%s\" -run \"%s\"", rpaExePath, rpaFilePath ); System.out.println("执行命令: " + command); // 创建进程并执行命令 Process process = Runtime.getRuntime().exec(command); // 创建线程捕获实输出流 StreamGobbler outputGobbler = new StreamGobbler( process.getInputStream(), "OUTPUT" ); StreamGobbler errorGobbler = new StreamGobbler( process.getErrorStream(), "ERROR" ); outputGobbler.start(); errorGobbler.start(); // 设置超间(30分钟)并等待执行完成 boolean finished = process.waitFor(30, TimeUnit.MINUTES); if (finished) { int exitCode = process.exitValue(); System.out.println("\nRPA执行完成,退出码: " + exitCode); // 根据退出码判断执行结果 if (exitCode == 0) { System.out.println("✅ RPA流程执行成功"); } else { System.out.println("❌ RPA流程执行失败,错误码: " + exitCode); } } else { System.err.println("⏰ RPA执行,强制终止进程"); process.destroyForcibly(); } } catch (IOException | InterruptedException e) { System.err.println("执行过程中发生错误:"); e.printStackTrace(); } } /** * 用于异步捕获命令行输出的线程类 */ static class StreamGobbler extends Thread { private InputStream inputStream; private String streamType; public StreamGobbler(InputStream inputStream, String streamType) { this.inputStream = inputStream; this.streamType = streamType; } @Override public void run() { try (BufferedReader reader = new BufferedReader( new InputStreamReader(inputStream, "GBK"))) { // 处理中文编码 String line; while ((line = reader.readLine()) != null) { System.out.printf("[RPA %s] %s%n", streamType, line); } } catch (IOException e) { System.err.println("读取" + streamType + "流出错:"); e.printStackTrace(); } } } } 执行结果 执行命令: "F:\lyin\LanYin_RPA\lyrpa.exe" -run "F:\lyin\lyin\在百度里模拟自动输入并点击搜索按钮.rpa" RPA执行完成,退出码: 0 ✅ RPA流程执行成功 但实际并未运行rpa 文件
最新发布
08-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值