诸位,最近学习java,写一个小程序,有这样一个问题:··
程序中检测windows中某个进程是否已经 存在或者运行在检测后,如果进程存在,则结束该进程
··请问我应该如何做呢???
应该参考哪个类???还望高手指教

解决方案 »

  1.   

    嘿,我再补充仔细一点比如我打开了记事本产生进程notepad.exe我如何在java程序中检测这个进程呢??
      

  2.   

    新出炉的东西结贴 结贴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");
    }

    }}
      

  3.   

    在 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 KC:\WINDOWS\system32>tasklist | find "flashget.EXE"C:\WINDOWS\system32>tasklistImage                        PID session          session#     RAM used
    ========================= ====== ================ ======== ============
    System Idle Process            0 Console                 0         28 K
    System                         4 Console                 0        168 K
    SMSS.EXE                     800 Console                 0         72 K
    CSRSS.EXE                    852 Console                 0     10,116 K
    WINLOGON.EXE                 876 Console                 0      3,132 K
    SERVICES.EXE                 932 Console                 0      2,124 K
      

  4.   


    这样可行以前写java调用浏览器进程差不多也是这样写的。
      

  5.   

    大概一定要用到Runtime,ProcessBuilder了。