import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
try {
String line;
Process p = Runtime.getRuntime().exec
(System.getenv("windir") +"\\system32\\"+"tasklist.exe");
BufferedReader input = new BufferedReader(new InputStreamReader(
p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line); // <-- Parse data here.
}
input.close();
} catch (Exception err) {
err.printStackTrace();
}
}
}
此程序的输出显示来自Windows任务管理器的进程列表.....但我还需要运行进程的描述???我怎么得到???
答案 0 :(得分:0)
tasklist.exe
使用可选参数/v
来表示详细信息。这将输出描述
实施例
taskmgr.exe 5648 Console 1 18,280 K Running 0:00:00 Windows Task Manager
您需要更新对exec()的调用以传递“/ v”。完整示例包括解析。
public static void main(String[] args) throws IOException {
String taskListExe = System.getenv("windir") + "\\system32\\" + "tasklist.exe";
Process p = Runtime.getRuntime().exec(new String[] { taskListExe, "/v" });
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
Pattern pattern = Pattern.compile("(.*?)\\s+(\\d+).*(\\d+:\\d+:\\d+)\\s+(.*?)");
String line;
while ((line = input.readLine()) != null) {
Matcher matcher = pattern.matcher(line);
if (matcher.matches()) {
System.out.println(String.format("%s, pid = %s, description = %s", matcher.group(1), matcher.group(2),
matcher.group(4)));
}
}
input.close();
}
输出
firefox.exe, pid = 3152, description = cmd - Get the windows running process description In java - Stack Overfl
taskmgr.exe, pid = 5648, description = Windows Task Manager
System Idle Process, pid = 0, description = N/A