彩虹导航个人博客
博客信息

java-启动和关闭.exe程序

0
发布时间:『 2020-04-24 09:34』  博客类别:Java核心基础  阅读(319) 评论(0)
// 启用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;
    }


关键字:   无
评论信息
暂无评论
发表评论
验证码: 
Powered by 彩虹导航 V3.0 Copyright © 2012-2016 彩虹导航网站 版权所有