【问题标题】:How to detect via Java whether a particular process is running under Windows?如何通过 Java 检测特定进程是否在 Windows 下运行?
【发布时间】:2010-02-23 12:56:21
【问题描述】:

好吧,标题几乎概括了这个问题。我唯一找到的是this 但我不确定这是否可行。

【问题讨论】:

  • 模棱两可的问题。您想弄清楚 Windows 进程是否是 Java 应用程序,还是想弄清楚 Java 应用程序如何判断它是否在 Windows 上运行?
  • 你需要处理什么?进程ID?可执行文件的名称?你开始这个过程了吗?你在等着看它是否完成了吗?基本上你为什么要检查它是否正在运行。根据情况有不同的技术。
  • @Stephen C 你是完全正确的,我不够清楚!我想知道如何以编程方式查看例如 notepad.exe 是否正在运行或 firefox.exe 是否正在运行等...希望能将其清除:)

标签: java


【解决方案1】:

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

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

【讨论】:

  • 谢谢你,正如我早先在评论中所说的那样,我还不够清楚。 :)
  • 是否有可能从windows句柄id中找出如何杀死正在运行的进程?
  • 谢谢你的好例子。有没有办法独立处理这个问题平台?我在 Linux 和 Apple 下也需要这个功能。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    取决于你需要知道什么!

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

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:

        我没有在非 Windows 系统中尝试过。 也许PID被4整除会提供一个线索 有关此 PID 属性的更多信息:About the pid of the process

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-05-28
          • 2011-12-08
          • 1970-01-01
          相关资源
          最近更新 更多