如何使用 Java 获取当前打开的窗口/进程列表? [英] How to get a list of current open windows/process with Java?

查看:32
本文介绍了如何使用 Java 获取当前打开的窗口/进程列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何使用Java获取本地机器当前打开的窗口或进程?

Does any one know how do I get the current open windows or process of a local machine using Java?

我想要做的是:列出当前打开的任务、窗口或进程打开,就像在 Windows 任务管理器中一样,但使用多平台方法 - 如果可能,只使用 Java.

What I'm trying to do is: list the current open task, windows or process open, like in Windows Taskmanager, but using a multi-platform approach - using only Java if it's possible.

推荐答案

这是从命令ps -e"解析进程列表的另一种方法:

This is another approach to parse the the process list from the command "ps -e":

try {
    String line;
    Process p = Runtime.getRuntime().exec("ps -e");
    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,那么您应该更改以下行:Process p = Runtime.getRun..."等...(第 3 行),如下所示:

If you are using Windows, then you should change the line: "Process p = Runtime.getRun..." etc... (3rd line), for one that looks like this:

Process p = Runtime.getRuntime().exec
    (System.getenv("windir") +"\system32\"+"tasklist.exe");

希望信息有帮助!

这篇关于如何使用 Java 获取当前打开的窗口/进程列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆