如何通过Java检测特定进程是否在Windows下运行?

如何通过Java检测特定进程是否在Windows下运行?

问题描述:

这个标题几乎总结了这个问题。我发现的唯一的事情是这个
但我不是确定这是否可行。

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.

您可以使用wmic实用程序检查正在运行的进程列表。

假设您要检查Windows的explorer.exe进程是否正在运行:

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();
}

参见 http://ss64.com/nt/wmic.html http://support.microsoft.com/servicedesks/webcasts/wc072402/listofsampleusage.asp ,了解一下你可以从wmic获得的一些例子......

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...