java在进程启动和关闭.exe进程

/**
     * @desc 启动进程
     * @author zp
     * @date 2018-3-29
     */
    public static void startProc(String processName) { 
         log.info("启动应用进程:" + processName);  
         if (StringUtils.isNotBlank(processName)) {  
             try {  
                 Desktop.getDesktop().open(new File(processName));  
             } catch (Exception e) {  
                 e.printStackTrace();  
                 log.error("应用进程:" + processName + "不存在!");  
             }  
         }   
    }
    /**
     * @desc 杀死进程
     * @author zp
     * @throws IOException 
     * @date 2018-3-29
     */
    public static void killProc(String processName) throws IOException {  
        log.info("关闭应用进程:" + processName);  
        if (StringUtils.isNotBlank(processName)) {  
            executeCmd("taskkill /F /IM " + processName);  
        } 
    }
    /**
     * @desc 执行cmd命令 
     * @author zp
     * @date 2018-3-29
     */
    public static String executeCmd(String command) throws IOException {  
        log.info("Execute command : " + command);  
        Runtime runtime = Runtime.getRuntime();  
        Process process = runtime.exec("cmd /c " + command);  
        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));  
        String line = null;  
        StringBuilder build = new StringBuilder();  
        while ((line = br.readLine()) != null) {  
            log.info(line);  
            build.append(line);  
        }  
        return build.toString();  
    }  
    /**
     * @desc 判断进程是否开启
     * @author zp
     * @date 2018-3-29
     */
    public static boolean findProcess(String processName) {
        BufferedReader bufferedReader = null;
        try {
            Process proc = Runtime.getRuntime().exec("tasklist -fi " + '"' + "imagename eq " + processName +'"');
            bufferedReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                if (line.contains(processName)) {
                    return true;
                }
            }
            return false;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (Exception ex) {}
            }
        }
    }

调用 

            //downfile = "D:\\DownFile\\DownFile.exe";
            String url = request.getParameter("downfile"); 
            String procName = url.substring(url.lastIndexOf("\\")+1,url.length());
            boolean exist= findProcess(procName); 
            try {
                if (exist) { 
                    // 存在,那么就先杀死该进程
                    killProc(procName);
                    // 在启动
                    startProc(url);
                }else {
                    startProc(url);
                }
            } catch (Exception e) {
                // TODO: handle exception
                log.error("重启/杀死提取进程失败。。。");  
            }

 

关键词:进程 processname string bufferedreader line new null url log 启动

相关推荐:

「小程序JAVA实战」小程序视频封面处理(47)

java实现微信扫一扫详解

微信小程序使用code换openid的方法(JAVA、SpringBoot)

java控制关闭linux进程

java中执行cmd命令

Java复习笔记-IO和NIO

Java 获取CPU、内存、外网IP等硬件信息

用多线程处理FTP上传

java信任所有证书

(转)java调用python脚本