windows上java進程工具類

檢測進程是否運行,尋找進程id,關閉進程等java

標籤: <無>apache

代碼片斷(1)[全屏查看全部代碼]

1. [代碼]Process工具類


packagecn.showclear.main.utils;
 
importjava.io.IOException;
importjava.util.Scanner;
 
importorg.apache.log4j.Logger;
 
/**
 * 進程工具類,windows版
 *
 * @author LuLihong
 * @date 2014-8-13
 */
publicclassProcessUtils {
    privatestaticfinalLogger log = Logger.getLogger(ProcessUtils.class);
    /**
     * 檢測某進程是否在運行
     * @param proName
     * @return
     */
    publicstaticbooleanisRunning(String proName) {
        try{
            Process process = Runtime.getRuntime().exec("tasklist");
            Scanner in =newScanner(process.getInputStream());
            while(in.hasNextLine()) {
                String p = in.nextLine();
                if(p.contains(proName)) {
                    returntrue;
                }
            }
            in.close();
        }catch(IOException e) {
            log.error(String.format("Check process[%s] running error: "+ e.getMessage(), proName));
        }
         
        returnfalse;
    }
     
    /**
     * 根據進程名尋找進程ID
     * @param proName
     * @return 不存在,返回-1
     */
    publicstaticintfindProcessId(String proName) {
        try{
            Process process = Runtime.getRuntime().exec("tasklist");
            Scanner in =newScanner(process.getInputStream());
            while(in.hasNextLine()) {
                String p = in.nextLine();
                if(p.contains(proName)) {
                    p = p.replaceAll("\\s+",",");
                    System.out.println(p);
                    String[] arr = p.split(",");
                    returnStringUtils.toInt(arr[1], -1);
                }
            }
            in.close();
        }catch(IOException e) {
            log.error(String.format("Find process[%s] id error: "+ e.getMessage(), proName));
        }
         
        return-1;
    }
     
    /**
     * 關閉某進程(根據PID)
     * @param pid
     * @return
     */
    publicstaticbooleankillProcess(intpid) {
        try{
            Runtime.getRuntime().exec("cmd.exe /c taskkill /f /pid "+ pid);
        }catch(IOException e) {
            log.error(String.format("Kill process[id=%d] error: "+ e.getMessage(), pid));
            returnfalse;
        }
         
        returntrue;
    }
     
    /**
     * 關閉進程(根據名稱)
     * @param proName
     * @return
     */
    publicstaticbooleankillProcess(String proName) {
        intpid = findProcessId(proName);
        if(pid == -1)returntrue;
        returnkillProcess(pid);
    }
     
     
    /**
     * 關閉某進程(根據名稱)直到真正關閉
     * @param proName
     * @return
     */
    publicstaticbooleankillProcessBlock(String proName) {
        intpid = findProcessId(proName);
        if(pid == -1)returntrue;
        do{
            killProcess(pid);
            try{
                Thread.sleep(100);
            }catch(InterruptedException e) {
            }
        }while(isRunning(proName));
         
        returntrue;
    }
     
    /**
     *  顯示全部進程
     */
    publicstaticvoidlistProcess() {
        try{
            Process process = Runtime.getRuntime().exec("tasklist");
         
            Scanner in =newScanner(process.getInputStream());
            while(in.hasNextLine()) {
                System.out.println(in.nextLine());
            }
            in.close();
        }catch(IOException e) {
        }
    }
     
    publicstaticvoidmain(String[] args) {
        longt1 = System.currentTimeMillis();
        listProcess();
        longt2 = System.currentTimeMillis();
        System.out.println("Used time: "+ (t2 - t1));
         
         
        String proName ="Foxmail.exe";
//      int pid = findProcessId(proName);
//      System.out.println(pid);
//      killProcess(pid);
          
         booleanb = killProcessBlock(proName);
         longt3 = System.currentTimeMillis();
         System.out.println("Used time: "+ (t3 - t2));
         System.out.println(b);
    }
     
}
相關文章
相關標籤/搜索