gpt4 book ai didi

Java,检查当前是否有任何进程 ID 在 Windows 上运行

转载 作者:可可西里 更新时间:2023-11-01 13:30:00 25 4
gpt4 key购买 nike

是否可以在 Windows 中检查来自 Java 的进程是否存在。

我有它可能的PID,我想知道它是否还在运行。

最佳答案

如何使用 Java 检查 pid 是否在 Windows 上运行:

Windows 任务列表命令:

DOS 命令 tasklist 显示一些关于正在运行的进程的输出:

C:\Documents and Settings\eric>tasklist

Image Name PID Session Name Session# Mem Usage
========================= ====== ================ ======== ============
System Idle Process 0 Console 0 28 K
System 4 Console 0 244 K
smss.exe 856 Console 0 436 K
csrss.exe 908 Console 0 6,556 K
winlogon.exe 932 Console 0 4,092 K
....
cmd.exe 3012 Console 0 2,860 K
tasklist.exe 5888 Console 0 5,008 K

C:\Documents and Settings\eric>

第二列是PID

您可以使用 tasklist 获取特定 PID 的信息:

tasklist /FI "PID eq 1300"

打印:

Image Name                   PID Session Name     Session#    Mem Usage
========================= ====== ================ ======== ============
mysqld.exe 1300 Console 0 17,456 K

C:\Documents and Settings\eric>

响应意味着 PID 正在运行。

如果您查询一个不存在的 PID,您会得到:

C:\Documents and Settings\eric>tasklist /FI "PID eq 1301"
INFO: No tasks running with the specified criteria.
C:\Documents and Settings\eric>

Java 函数可以自动执行上述操作

此功能仅适用于具有可用的 tasklist 的 Windows 系统。

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class IsPidRunningTest {

public static void main(String[] args) {

//this function prints all running processes
showAllProcessesRunningOnWindows();

//this prints whether or not processID 1300 is running
System.out.println("is PID 1300 running? " +
isProcessIdRunningOnWindows(1300));

}

/**
* Queries {@code tasklist} if the process ID {@code pid} is running.
* @param pid the PID to check
* @return {@code true} if the PID is running, {@code false} otherwise
*/
public static boolean isProcessIdRunningOnWindows(int pid){
try {
Runtime runtime = Runtime.getRuntime();
String cmds[] = {"cmd", "/c", "tasklist /FI \"PID eq " + pid + "\""};
Process proc = runtime.exec(cmds);

InputStream inputstream = proc.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
String line;
while ((line = bufferedreader.readLine()) != null) {
//Search the PID matched lines single line for the sequence: " 1300 "
//if you find it, then the PID is still running.
if (line.contains(" " + pid + " ")){
return true;
}
}

return false;
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("Cannot query the tasklist for some reason.");
System.exit(0);
}

return false;

}

/**
* Prints the output of {@code tasklist} including PIDs.
*/
public static void showAllProcessesRunningOnWindows(){
try {
Runtime runtime = Runtime.getRuntime();
String cmds[] = {"cmd", "/c", "tasklist"};
Process proc = runtime.exec(cmds);
InputStream inputstream = proc.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
String line;
while ((line = bufferedreader.readLine()) != null) {
System.out.println(line);
}
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("Cannot query the tasklist for some reason.");
}
}
}

上面的 Java 代码打印所有正在运行的进程的列表,然后打印:

is PID 1300 running? true

关于Java,检查当前是否有任何进程 ID 在 Windows 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2533984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com