链接:
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; }