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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2318220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:03:39  来源:igfitidea点击:

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

java

提问by gmunk

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

好吧,标题几乎总结了这个问题。我发现的唯一一件事就是这个,但我不确定那是不是要走的路。

采纳答案by Philippe

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 :

您可以使用 wmic 实用程序来检查正在运行的进程列表。
假设您要检查 windows 的 explorer.exe 进程是否正在运行:

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.htmlor http://support.microsoft.com/servicedesks/webcasts/wc072402/listofsampleusage.aspfor some example of what you can get from wmic...

请参阅http://ss64.com/nt/wmic.htmlhttp://support.microsoft.com/servicedesks/webcasts/wc072402/listofsampleusage.asp了解您可以从 wmic 获得的一些示例...

回答by Geo

os.name should do it. More information here

os.name 应该这样做。更多信息在这里

回答by Thorbj?rn Ravn Andersen

Depends on what you need to know it for!

取决于你需要知道什么!

Most information can be derived from the default runtime properties, without actually checking the operating system properties.

大多数信息可以从默认运行时属性中获得,而无需实际检查操作系统属性。

Have a look at what http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html#getProperties()provides:

看看http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html#getProperties()提供了什么:

java.version    Java Runtime Environment version
java.vendor Java Runtime Environment vendor
java.vendor.url Java vendor URL
java.home   Java installation directory
java.vm.specification.version   Java Virtual Machine specification version
java.vm.specification.vendor    Java Virtual Machine specification vendor
java.vm.specification.name  Java Virtual Machine specification name
java.vm.version Java Virtual Machine implementation version
java.vm.vendor  Java Virtual Machine implementation vendor
java.vm.name    Java Virtual Machine implementation name
java.specification.version  Java Runtime Environment specification version
java.specification.vendor   Java Runtime Environment specification vendor
java.specification.name Java Runtime Environment specification name
java.class.version  Java class format version number
java.class.path Java class path
java.library.path   List of paths to search when loading libraries
java.io.tmpdir  Default temp file path
java.compiler   Name of JIT compiler to use
java.ext.dirs   Path of extension directory or directories
os.name Operating system name
os.arch Operating system architecture
os.version  Operating system version
file.separator  File separator ("/" on UNIX)
path.separator  Path separator (":" on UNIX)
line.separator  Line separator ("\n" on UNIX)
user.name   User's account name
user.home   User's home directory
user.dir    User's current working directory

回答by saugata

You are trying to determine if a process you created is still running?

您正在尝试确定您创建的进程是否仍在运行?

  1. If you have the PID the link you posted will do.
  2. If the other process is also your own (your code), you can make it get exclusive lock on a file; try locking it from the other code if it succeeds the other process is not running.
  1. 如果您有 PID,您发布的链接就可以了。
  2. 如果另一个进程也是你自己的(你的代码),你可以让它获得一个文件的独占锁;如果其他进程没有运行成功,请尝试将其与其他代码锁定。

回答by balalakshmi

I have not tried in non Windows based systems. perhaps the PID divisibility by 4 will provide a clue More info on this PID propery here : About the pid of the process

我还没有在非基于 Windows 的系统中尝试过。也许 PID 可被 4 整除将提供线索有关此 PID 属性的更多信息:关于进程的 pid

and here http://blogs.msdn.com/oldnewthing/archive/2008/02/28/7925962.aspx

在这里 http://blogs.msdn.com/oldnewthing/archive/2008/02/28/7925962.aspx