日期:2014-05-20  浏览次数:20802 次

java中检测windows进程的问题,新手求教

诸位,最近学习java,写一个小程序,有这样一个问题:

··
程序中检测windows中某个进程是否已经 存在或者运行

在检测后,如果进程存在,则结束该进程
··

请问我应该如何做呢???
应该参考哪个类???

还望高手指教


------解决方案--------------------
新出炉的东西

结贴 结贴

Java code
package CSDNTest;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ProcessHandler {

    /**
     * @author coldanimal; ProcessHandler windowns version.
     */
    public static boolean findRunningProcess(String processName) {
        String platform = System.getProperty("os.name");
        if (platform.contains("Windows")) {
            return findRunningWindowsProcess(processName);
        } else if (platform.contains("Linux")) {
            return findRunningLinuxProcess(processName);
        } else {
            throw new RuntimeException("Unknown platform " + platform);
        }
    }
   
    private static boolean findRunningLinuxProcess(String processName) {
        return false;
    }

    private static boolean findRunningWindowsProcess(String processName) {
        BufferedReader bufferedReader = null;
        Process proc = null;
        try {
            proc = Runtime.getRuntime().exec("tasklist /FI \"IMAGENAME eq " + processName + "\"");
            bufferedReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line;
            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) {
                }
            }
            if (proc != null) {
                try {
                    proc.destroy();
                } catch (Exception ex) {
                }
            }
        }
    }
    
    public static boolean killRunningProcess(String processName){
        String platform = System.getProperty("os.name");
        if(platform.contains("Windows")){
            return killRunningWindowsProcess(processName);
        }else if(platform.contains("Linux")){
            return false;
        }
        throw new RuntimeException("Unkown platform " + platform);
    }
    
    private static boolean killRunningWindowsProcess(String processName){
        try {
            Runtime.getRuntime().exec("taskkill /IM " + processName);
            System.out.println("kill process successful");
            System.out.println("Process " + processName + " was killed. Mission completed.");
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("kill process fail");
            System.out.println("Misson failed.");
            return false;
        }
    }
    
    public static void main(String[] args) {
        if(ProcessHandler.findRunningProcess("notepad.exe")){
            ProcessHandler.killRunningProcess("notepad.exe");
        }
        
    }

}

------解决方案--------------------
在 XP 下有 tasklist.exe 它会返回当前运行的进程。你再用程序读取这个输出就可以解析查找里面的 notepad.exe.

Process p = Runtime.getRuntime().execute("cmd.exe /c tasklist.exe");

InputStream input = p.getInputStream();

...

我们用 cmd.exe /c tasklist.exe | find "flashget.exe" 这时区分大小写的,不合适,可能要自己用代码不区分大小写的方式去查找 notepad.exe 。


C:\WINDOWS\system32>tasklist | find "flashget.exe"
flashget.exe 4564 Console 0 8,752 K