使用apache.commons.exec执行系统命令

 阻塞执行方式:

@Slf4j
public class CmdUtils {

    /**
     * 执行系统命令, 返回执行结果
     *
     * @param cmd      需要执行的命令
     * @param distPath 执行命令的子进程的工作目录, null 表示和当前主进程工作目录相同
     */
    public static String execCmd(String cmd, String distPath) {
        File file = new File(distPath);
        int exitValue = 1;
        try {

            // 执行命令, 返回一个子进程对象(命令在子进程中执行)
            log.info("开始执行命令和目录:[{}],[{}]" , cmd,distPath);
            CommandLine cmdLine = CommandLine.parse(cmd);
            DefaultExecutor executor = new DefaultExecutor();
            executor.setWorkingDirectory(file);
            executor.setExitValues(null);
            ExecuteWatchdog watchdog = new ExecuteWatchdog(600000);
            executor.setWatchdog(watchdog);

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
            PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, errorStream);
            executor.setStreamHandler(streamHandler);
            exitValue = executor.execute(cmdLine);
            //获取程序外部程序执行结果
            String out = outputStream.toString("utf8");
            String error = errorStream.toString("utf8");
            log.info("执行命令处理结果exitValue:[{}],out:[{}],error:[{}]",exitValue,out,error);
        } catch (Exception e) {
            log.error("执行批处理命令异常[{}]", e);
            throw new BusinessException(RetCode.CMD_ERROR);
        }
        log.info("结束执行命令和目录:[{}],[{}][{}s]" , cmd,distPath);
        if(exitValue != 0){
            log.error("执行批处理命令失败");
            throw new BusinessException(RetCode.CMD_ERROR);
        }
        return String.valueOf(exitValue);
    }


如果不想在执行外部命令的时候,把当前线程阻塞,可以使用DefaultExecuteResultHandler处理外部命令执行的结果,释放当前线程。

@Slf4j
public class CmdUtils {

    /**
     * 执行系统命令, 返回执行结果
     *
     * @param cmd      需要执行的命令
     * @param distPath 执行命令的子进程的工作目录, null 表示和当前主进程工作目录相同
     */
    public static boolean execCmd(String cmd, String distPath) {
        File file = new File(distPath);
        boolean result = true;
        if(!file.exists()){
           file.mkdir();
        }
        int exitValue = 1;
        try {
            // 执行命令, 返回一个子进程对象(命令在子进程中执行)
            log.info("开始执行命令和目录:[{}],[{}]" , cmd,distPath);
            CommandLine cmdLine = CommandLine.parse(cmd);
            DefaultExecutor executor = new DefaultExecutor();
            executor.setWorkingDirectory(file);
            executor.setExitValues(null);
            ExecuteWatchdog watchdog = new ExecuteWatchdog(600000);
            executor.setWatchdog(watchdog);

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
            PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, errorStream);
            executor.setStreamHandler(streamHandler);
            //非阻塞
            DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
            try {
                executor.execute(cmdLine, resultHandler);
                resultHandler.waitFor();
            } catch (Exception e) {
                result = false;
                e.printStackTrace();
            }
        } catch (Exception e) {
            log.error("执行批处理命令异常[{}]", e);
            throw new BusinessException(RetCode.CMD_ERROR);
        }
        log.info("结束执行命令和目录:[{}],[{}][{}s]" , cmd,distPath);
        if(exitValue != 0){
            log.error("执行批处理命令失败");
            throw new BusinessException(RetCode.CMD_ERROR);
        }
        return result;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值