How to detect via Java whether a particular process is running under Windows?

后端 未结 5 676
南笙
南笙 2020-12-06 17:58

Well the title pretty much sums the question. The only thing I found is this but I\'m not sure if thats the way to go.

5条回答
  •  孤街浪徒
    2020-12-06 18:46

    You can use the wmic utility to check the list of running processes.
    Suppose you want to check if the windows' explorer.exe process is running :

    String line;
    try {
        Process proc = Runtime.getRuntime().exec("wmic.exe");
        BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        OutputStreamWriter oStream = new OutputStreamWriter(proc.getOutputStream());
        oStream .write("process where name='explorer.exe'");
        oStream .flush();
        oStream .close();
        while ((line = input.readLine()) != null) {
            System.out.println(line);
        }
        input.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    

    See http://ss64.com/nt/wmic.html or http://support.microsoft.com/servicedesks/webcasts/wc072402/listofsampleusage.asp for some example of what you can get from wmic...

提交回复
热议问题