import java.io.BufferedReader; import java.io.InputStreamReader; /** * JAVA检测指定的进程是否在运行。 * * @author JAVA世纪网(java2000.net, laozizhu.com) */ public class TestFindProcessOfExe { public static void main(String[] args) { System.out.println(findProcess("eclipse.exe")); } /** * 检测程序。 * * @param processName 线程的名字,请使用准确的名字 * @return 找到返回true,没找到返回false */ public static boolean findProcess(String processName) { BufferedReader bufferedReader = null; try { //Linux下使用ps -ef //Process proc = Runtime.getRuntime().exec(new String[]{"/bin/sh","-c","ps -ef|grep -v /"grep/"|grep java "}); 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) {} } } } }