Beyond Compare比较工具的使用

本文介绍了一种在Java中通过调用命令行工具的方法,并提供了详细的代码实现。此外,还探讨了如何处理Windows环境下的一些常见异常情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、cmd命令行运行

二、Java调用

utils工具类:CmdProcUtil.java

package com.huaqingth.safetyMonitor.utils;

import java.io.ByteArrayOutputStream;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.PumpStreamHandler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class CmdProcUtil {

    public static Log log = LogFactory.getLog(CmdProcUtil.class);
    public static String MYSQL_NAME = "mysql";

    public static void restartMySQL() {
        if (SysTools.isWinSys()) {
            proc("net stop " + MYSQL_NAME);
            proc("net start " + MYSQL_NAME);
        } else {
            proc("service " + MYSQL_NAME + " restart");
        }
    }

    public static boolean proc(String cmd) {
        boolean flag = false;
        Process pr = null;
        try {
            pr = Runtime.getRuntime().exec(cmd);
            new PrintStream(pr.getInputStream()).start();
            pr.waitFor();
            //Thread.sleep(1000 * 10);
            int exitValue = pr.exitValue();
            flag = exitValue == 0;
        } catch (Exception e) {
            log.error("[CmdProcUtil::proc] " + e.getMessage());
        }finally{
            if(pr!= null)pr.destroy();
        }
        return flag;
    }

    public static boolean procByArgs(String[] args){
        boolean flag = false;
        Process pr = null;
        try {
            pr = Runtime.getRuntime().exec(args);
            new PrintStream(pr.getInputStream()).start();
            pr.waitFor();
            //Thread.sleep(1000 * 10);
            int exitValue = pr.exitValue();
            flag = exitValue == 0;
        } catch (Exception e) {
            log.error("[CmdProcUtil::procByArgs] " + e.getMessage());
        }finally{
            if(pr!= null)pr.destroy();
        }
        return flag;
    }

    public static boolean CommPorcByArgs(String arg, String[] args){
        boolean flag = true;
        try{
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
            CommandLine commandline = new CommandLine(arg);
            commandline.addArguments(args, false);
            //ExecuteWatchdog watchDog = new ExecuteWatchdog(60*1000);
            DefaultExecutor exec = new DefaultExecutor();
            exec.setExitValues(null);
            PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream,errorStream);
            exec.setStreamHandler(streamHandler);
            //exec.setWatchdog(watchDog);
            exec.execute(commandline);
            /*String out = outputStream.toString("gbk");
            String error = errorStream.toString("gbk");
            System.out.println(out+error);*/
        }catch(Exception e){
            flag = false;
        }
        return flag;
    }

    public static boolean CommProc(String cmd){
        boolean flag = false;
        try {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
            CommandLine commandline = CommandLine.parse(cmd);
            //ExecuteWatchdog watchDog = new ExecuteWatchdog(60*1000);
            DefaultExecutor exec = new DefaultExecutor();
            exec.setExitValues(null);
            PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream,errorStream);
            exec.setStreamHandler(streamHandler);
            //exec.setWatchdog(watchDog);
            exec.execute(commandline);
            String out = outputStream.toString("gbk");
            String error = errorStream.toString("gbk");
            System.out.println(out+error);
        } catch (Exception e) {
            log.info("CommProc: "+e.getMessage());
        } 
        return flag;
    }

    public static boolean CommProcNoOut(String cmd){
        boolean flag = false;
        try {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
            CommandLine commandline = CommandLine.parse(cmd);
            //ExecuteWatchdog watchDog = new ExecuteWatchdog(60*1000);
            DefaultExecutor exec = new DefaultExecutor();
            exec.setExitValues(null);
            PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream,errorStream);
            exec.setStreamHandler(streamHandler);
            //exec.setWatchdog(watchDog);
            exec.execute(commandline);
        } catch (Exception e) {
            log.info("CommProcNoOut: "+e.getMessage());
        } 
        return flag;
    }
}

class PrintStream extends Thread {
    java.io.InputStream __is = null;

    public PrintStream(java.io.InputStream is) {
        __is = is;
    }

    public void run() {
        try {
            while (this != null) {
                int _ch = __is.read();
                if (_ch != -1){
                    System.out.print((char) _ch);
                }else{
                    break;
                }
            }
        } catch (Exception e) {
            CmdProcUtil.log.error(e.getMessage());
        }
    }
}

 调用:

    String bcompareDir = "E:/安装工具/Beyond_Compare/BeyondCompare4/";
        //获取本地临时文件路径
        String fileDir = FILEDIR+url.hashCode()+"/"+temperType+"/";
        //输入命令行
        String cmd =  bcompareDir + "BComp.exe @" + bcompareDir + "test.txt "+fileDir+"old.txt "+fileDir+"new.txt "+fileDir+"diff.html";
        CmdProcUtil.proc(cmd);

======================================================================

三、异常处理

1. 报错
[CmdProcUtil::proc] Cannot run program "tesseract": CreateProcess error=14001, 应用程序无法启动,因为应用程序的并行配置不正确。有关详细信息,请参阅应用程序事件日志,或使用命令行 sxstrace.exe 工具。

解释
程序调用BComp时报该错误,但在cmd中可调用,也可以直接打开BComp.exe
原因:
Windows Modules Installer服务被禁用;
解决办法:
开始 - 运行(输入services.msc)- 确定或回车,打开:服务(本地);
我们在服务(本地)窗口找到:Windows Modules Installer服务,查看是否被禁用;
如果Windows Modules Installer服务被禁用,我们必须把它更改为启用 - 手动。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值