import java.io.BufferedReader;
import java.io.InputStreamReader;
public class CheckJavaProcess {
public static void main(String[] args) {
try {
// 使用操作系统命令来查看Java进程
// 在Windows上使用 "tasklist", 在Linux或Mac上使用 "ps"
String os = System.getProperty("os.name").toLowerCase();
String command;
if (os.contains("win")) {
command = "tasklist";
} else {
command = "ps -ef | grep java";
}
Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
System.getProperty("os.name")
获取当前操作系统的名称,并根据不同的操作系统选择不同的命令。tasklist
命令来列出所有进程。ps -ef | grep java
命令来查找包含 "java" 的进程。Runtime.getRuntime().exec()
执行命令,并使用 BufferedReader
读取命令的输出。这样可以方便地查看当前系统中运行的 Java 进程。
上一篇:java类
下一篇:java 字符串转时间
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站