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("重啟/殺死提取程序失敗。。。");  
            }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM