Home > Java > javaTutorial > How Can I List Active Windows or Processes in Java Cross-Platform?

How Can I List Active Windows or Processes in Java Cross-Platform?

Barbara Streisand
Release: 2024-12-14 08:22:11
Original
233 people have browsed it

How Can I List Active Windows or Processes in Java Cross-Platform?

How to Obtain a List of Active Windows or Processes Using Java

Question:

Can anyone assist me in retrieving a list of currently open windows or processes on a local machine using Java? I aim to create a cross-platform solution similar to the Windows Task Manager's listing of active processes.

Answer:

An alternative approach to extracting the process list from the "ps -e" command:

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();
}
Copy after login

For Windows systems, modify the code as follows:

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

This approach should provide the desired list of running windows or processes.

The above is the detailed content of How Can I List Active Windows or Processes in Java Cross-Platform?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template