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

好吧,标题几乎总结了这个问题。 我发现的唯一的事情是这个,但我不确定这是不是这样。

您可以使用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(); } 

有关您可以从wmic获得的一些示例,请参阅http://ss64.com/nt/wmic.html或http://support.microsoft.com/servicedesks/webcasts/wc072402/listofsampleusage.asp …

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

取决于你需要知道什么!

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

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

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

  1. 如果你有PID,你发布的链接就可以了。
  2. 如果其他进程也是您自己的(您的代码),您可以使它获得对文件的独占锁定; 尝试将其从其他代码锁定,如果成功则其他进程未运行。

我没有尝试过非基于Windows的系统。 也许4的PID可分性将提供一个线索这里有关于 PID的更多信息: 关于过程的pid

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