哎哟,不错哦

导航

java 检查进程是否存在

以nginx进程为例子

 

private final static String NAME_STRING = "nginx.exe";

//传入进程名称processName
	public static boolean findProcess() {
		BufferedReader bufferedReader = null;
	    try {
	        Process proc = Runtime.getRuntime().exec("tasklist -fi " + '"' + "imagename eq " + NAME_STRING +'"');
	        bufferedReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
	        String line = null;
	        while ((line = bufferedReader.readLine()) != null) {
	            if (line.contains(NAME_STRING)) {
	                return true;
	            }
	        }
	        return false;
	    } catch (Exception ex) {
	        ex.printStackTrace();
	        return false;
	    } finally {
	        if (bufferedReader != null) {
	            try {
	                bufferedReader.close();
	            } catch (Exception ex) {}
	        }
	    }
	}
	

  

posted on 2019-05-02 22:11  哎哟,不错哦  阅读(733)  评论(0编辑  收藏  举报