除使用JAVA在后台运行的应用程序外,如何获取任务管理器等当前正在运行的应用程序的列表? 我有找到的这段代码-https://stackoverflow.com/a/41634959/11297873:
Process process = new ProcessBuilder("tasklist.exe", "/fo", "csv", "/nh").start();
new Thread(() -> {
Scanner sc = new Scanner(process.getInputStream());
if (sc.hasNextLine()) sc.nextLine();
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] parts = line.split(",");
String unq = parts[0].substring(1).replaceFirst(".$", "");
String pid = parts[1].substring(1).replaceFirst(".$", "");
System.out.println(unq + " " + pid);
}
}).start();
process.waitFor();
System.out.println("Done");
我不想显示诸如System,Windows,Intel等的后台应用程序。
答案 0 :(得分:1)
您这里需要名为Get-Process的powershell cmdlet。
Process process = new ProcessBuilder("powershell","\"gps| ? {$_.mainwindowtitle.length -ne 0} | Format-Table -HideTableHeaders name, ID").start();
new Thread(() -> {
Scanner sc = new Scanner(process.getInputStream());
if (sc.hasNextLine()) sc.nextLine();
while (sc.hasNextLine()) {
String line = sc.nextLine();
System.out.println(line);
}
}).start();
process.waitFor();
System.out.println("Done");
输出如下:
ApplicationFrameHost 15592
chrome 12920
cmd 21104
debian 13264
Far 3968
firefox 17240
HxOutlook 4784
idea64 13644
MicrosoftEdge 8024
MicrosoftEdgeCP 13908
MicrosoftEdgeCP 14604
mstsc 24636
notepad++ 9956
OUTLOOK 9588
pycharm64 6396
rider64 10468
Teams 11540
Telegram 16760
Done
答案 1 :(得分:0)
所以我拿了你的代码
Process process = new ProcessBuilder("tasklist.exe", "/fo", "csv", "/nh").start();
new Thread(() -> {
Scanner sc = new Scanner(process.getInputStream());
if (sc.hasNextLine()) sc.nextLine();
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] parts = line.split(",");
String unq = parts[0].substring(1).replaceFirst(".$", "");
String pid = parts[1].substring(1).replaceFirst(".$", "");
System.out.println(unq + " " + pid);
}
}).start();
process.waitFor();
System.out.println("Done");
并将其更改为
Process process = new ProcessBuilder("tasklist.exe", "/fo", "csv", "/nh").start();
new Thread(() -> {
Scanner sc = new Scanner(process.getInputStream());
if (sc.hasNextLine()) sc.nextLine();
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] parts = line.split(",");
String unq = parts[0].substring(1).replaceFirst(".$", "");
String pid = parts[1].substring(1).replaceFirst(".$", "");
System.out.println(line);
}
}).start();
process.waitFor();
System.out.println("Done");
基本上打印整个行,而不仅仅是“ unq”和“ pid”
我看到的是整个印刷线看起来像这样:
"System Idle Process","0","Services","0","8 K"
前2个字符串分别为“ unq”和“ pid”,而第3个字符串是进程的规范,第5个字符串是其使用的内存量。
据我所知,似乎第四个字符串可能正是您要寻找的。它始终是0或1,从我可以告诉前台进程的总值是1,因此我假设如果您将其检查为0,则可以得到这样的后台进程:
Process process = new ProcessBuilder("tasklist.exe", "/fo", "csv", "/nh").start();
new Thread(() -> {
Scanner sc = new Scanner(process.getInputStream());
//if (sc.hasNextLine()) sc.nextLine();
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] parts = line.split(",");
String unq = parts[0].substring(1).replaceFirst(".$", "");
String pid = parts[1].substring(1).replaceFirst(".$", "");
if(parts[3].substring(1).replaceFirst(".$", "").equals("0")) {
System.out.println(unq + " " + pid);
}
}
}).start();
process.waitFor();
System.out.println("Done");
仍然不是100%肯定,我无法通过谷歌搜索找到很多东西。但到目前为止看来似乎是这样