java中检测windows进程的问题,新手求教 诸位,最近学习java,写一个小程序,有这样一个问题:··程序中检测windows中某个进程是否已经 存在或者运行在检测后,如果进程存在,则结束该进程··请问我应该如何做呢???应该参考哪个类???还望高手指教 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 嘿,我再补充仔细一点比如我打开了记事本产生进程notepad.exe我如何在java程序中检测这个进程呢?? 新出炉的东西结贴 结贴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 KC:\WINDOWS\system32>tasklist | find "flashget.EXE"C:\WINDOWS\system32>tasklistImage PID session session# RAM used========================= ====== ================ ======== ============System Idle Process 0 Console 0 28 KSystem 4 Console 0 168 KSMSS.EXE 800 Console 0 72 KCSRSS.EXE 852 Console 0 10,116 KWINLOGON.EXE 876 Console 0 3,132 KSERVICES.EXE 932 Console 0 2,124 K 这样可行以前写java调用浏览器进程差不多也是这样写的。 大概一定要用到Runtime,ProcessBuilder了。 使用 org.apache.commons.net.ftp 进行文件下载的问题 netbeans及mysql数据库的高手请进 java 线程能够实现AJAX的功能么 面试,求各位帮帮忙了 将图片以字节流的形式直接写到jsp文件中,高手来帮忙。 如何用socket得到页面表单post的数据 再问:怎么消除在applet的paint()中画图时抖动的问题!! 请问各位仁兄下载的J2SDK1.42都是多大呀? (送分发发88)一个java应用程序的表单布置的小问题,希望大家帮忙? 这是怎么回事? 题目如下 请教 java中如何输出一个变量的反值?
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");
}
}}
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
这样可行以前写java调用浏览器进程差不多也是这样写的。