java - java中获取windows运行进程描述

标签 java cmd

       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 任务管理器中的进程列表.....但我还需要正在运行的进程的描述???我怎样才能得到它???

最佳答案

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      

关于java - java中获取windows运行进程描述,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42502207/

相关文章:

java - 如何让我的 Java 程序读取我想要的文本文件?

Java Swing 击键 : how to make CTRL-modifier work

batch-file - 如何在不基于系统时钟的CMD中生成随机数

batch-file - 将 SID 存储在变量中

java - 写入 CSV 文件时,之前的字符串将被覆盖

java - 如何在 Android 上仅更新 Firebase 数据库上的特定字段

java - sshtools.SftpClient.put 失败,错误代码 "No such file"

c# - 在命令行中使用 MSTSC 传输文件

windows - 在 PowerShell 和命令提示符上选择某些内容时避免暂停任务

postgresql - 如何通过 CLI 连接到 PostgreSQL?