获取java中的活动程序列表

Par*_*vil 8 java cmd windows-10

我需要使用java检索当前打开的程序列表.以下代码为我提供了所有活动程序的列表,包括任何后台进程,但我只需要一个活动程序列表.

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);
        }
    input.close();
} catch (Exception err) {
    err.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

我不会意识到目前正在打开哪些程序,因此无法通过搜索一系列名称找到它,因为我看到有些人推荐这种方法.

通过活动程序,我指的是用户可以通过窗口进行交互的任何程序.任务管理器窗口已经将程序(在详细视图中)拆分为应用程序和后台进程,我希望能够检索将在应用程序部分下排序的任何程序.

Kha*_*l M 1

添加此命令行

String command="powershell -command \"get-Process cmd | format-table mainwindowtitle\"";
Run Code Online (Sandbox Code Playgroud)

并在这里使用它

Process p = Runtime.getRuntime().exec(command);
Run Code Online (Sandbox Code Playgroud)