如何检查进程是否在 Windows 上运行? [英] How to check if a process is running on Windows?

查看:50
本文介绍了如何检查进程是否在 Windows 上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做的是一个程序来了解它是否在 Windows 上处于活动状态以在 Java 中使用它.我在网上查了一下,找到了两种不同的解决方案:

What I am trying to do it is a program to know if a process it is active on Windows to work with it in Java. I looked on the Internet and found two different solutions:

但这根本不是我想要的.

But it is not at all what I am looking for.

根据选项 1,我想要一种方法来引用进程(并检测它是否处于活动状态/正在运行),但无需在完整的 tasklist 中进行搜索,然后对其进行搜索.我正在寻找是否有直接的方法可以做到这一点.

According to the option 1, I would like a method to make reference to the process (and detect if it is active/running) but without searching in the full tasklist and then searching on it. I am searching if there is a direct method to do that.

我也向任务列表添加了一个过滤器,但找不到任何过滤器来只获取我正在寻找的进程.我在命令行上使用 tasklist/? 看到了所有选项.

I also though to add a filter to the tasklist but could not find any filter to only get the process I am looking for. I saw all the options using tasklist /? on my command line.

然后我搜索了有关第二个选项和 wmic(我以前从未听说过)的信息,似乎 wmic 允许您在命令行上执行任务(正确如果我错了,请联系我).

Then I searched information about the second option and wmic (that I never heard before) and it seems that wmic allows you to execute tasks on the command line (correct me if I am wrong please).

所以,我有两个问题:

  • 是否有直接的方法可以知道进程是否在使用 Java 的 Windows 上处于活动状态?尽量避免在完整的任务列表中搜索或使用 wmic.

如果不可能,如果我们谈论高效编程,我上面提到的两个选项中哪个更好?

If it is impossible, what of the two options that I put above would be better if we talk about efficient programming?

提前致谢!

推荐答案

没有直接的方法来查询一般进程,因为每个操作系统对它们的处理方式不同.
您有点坚持使用代理,例如直接操作系统命令...

There is no direct way to query general processes as each OS handles them differently.
You kinda stuck with using proxies such as direct OS commands...

可以使用带有 /fi 参数的 tasklist.exe 查找特定进程.
例如:tasklist.exe/nh/fi "Imagename eq chrome.exe"
请注意强制性的双引号.
语法&MS Technet 站点上提供了用法.

You can however, find a specific process using tasklist.exe with /fi parameter.
e.g: tasklist.exe /nh /fi "Imagename eq chrome.exe"
Note the mandatory double quotes.
Syntax & usage are available on MS Technet site.

同样的例子,在 Java 中过滤chrome.exe":

Same example, filtering for "chrome.exe" in Java:

String findProcess = "chrome.exe";
String filenameFilter = "/nh /fi \"Imagename eq "+findProcess+"\"";
String tasksCmd = System.getenv("windir") +"/system32/tasklist.exe "+filenameFilter;

Process p = Runtime.getRuntime().exec(tasksCmd);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));

ArrayList<String> procs = new ArrayList<String>();
String line = null;
while ((line = input.readLine()) != null) 
    procs.add(line);

input.close();

Boolean processFound = procs.stream().filter(row -> row.indexOf(findProcess) > -1).count() > 0;
// Head-up! If no processes were found - we still get: 
// "INFO: No tasks are running which match the specified criteria."

这篇关于如何检查进程是否在 Windows 上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆