java-启动和关闭.exe程序

链接:

https://www.cnblogs.com/pengpengzhang/p/8675740.html

https://blog.csdn.net/ZHANGHUI3239619/article/details/78808129

例:

// 启用exe客户端
    @RequestMapping("/startUpXFExe")
    @ResponseBody
    public ResponseResult startUpXFExe(ModelMap map, HttpServletRequest request) {
        ResponseResult responseResult = new ResponseResult(ResponseResult.FAILURECODE);
        String exePathXF = SysConfigItemValue.getValue("exePathXF");//exe存放路径
        if (StringUtils.isNotBlank(exePathXF)) {
            String procName = "iflytek.Court.Client.exe";
            String result = checkProcess(procName);//检查exe进程
            if (result.isEmpty()) {
                try {  
                    //启动exe执行程序
                    Desktop.getDesktop().open(new File(exePathXF));  
                    responseResult.setCode(ResponseResult.SUCCESSCODE);
                    //responseResult.setMsg("程序启动成功。");
                } catch (Exception e) {  
                    e.printStackTrace(); 
                    responseResult.setMsg("程序:" + exePathXF + "不存在!");
                    logger.error("程序:" + exePathXF + "不存在!");  
                }  
            }else {
                responseResult.setMsg(result);
            }
        }   
        return responseResult;
    }
    
    /**
     * 检查进程是否存在,存在则杀死进程
     * @param procName
     */
    public String checkProcess(String procName) {
        String result = "";
        //判断是否存在进程
        Boolean existProc = false;
        BufferedReader bufferedReader = null;
        try {
            Process proc = Runtime.getRuntime().exec("tasklist -fi " + '"' + "imagename eq " + procName +'"');
            bufferedReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                if (line.contains(procName)) {
                    existProc = true;//存在
                }
            }
        } catch (Exception ex) {
            result = "查询程序进程异常:"+ex.getMessage();
            logger.error("查询程序进程异常:"+ex.getMessage());  
            return result;
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (Exception ex) {}
            }
        }

        // 存在,则先杀死该进程
        if (existProc) { 
            BufferedReader br = null;
             try {
                 if (StringUtils.isNotBlank(procName)) {  
                     //执行cmd命令 
                     String command = "taskkill /F /IM " + procName; 
                     Runtime runtime = Runtime.getRuntime();  
                     Process process = runtime.exec("cmd /c " + command);  
                     br = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));  
                     String line = null;  
                     StringBuilder build = new StringBuilder();  
                     while ((line = br.readLine()) != null) {   
                         build.append(line);  
                     }  
                 } 
             } catch (Exception e) {
                 result = "关闭程序进程异常:"+e.getMessage();
                 logger.error("关闭程序进程异常:"+e.getMessage()); 
                 return result;
             } finally {
                 if (br != null) {
                     try {
                         br.close();
                     } catch (Exception ex) {}
                 }
             }
        }
        return result;
    }

 

转载于:https://www.cnblogs.com/lijianda/p/10138944.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值