Asked  7 Months ago    Answers:  5   Viewed   34 times

Does any one know how do I get the current open windows or process of a local machine using 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.

 Answers

41

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

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");

Hope the info helps!

Tuesday, June 1, 2021
 
Grzegorz
answered 7 Months ago
40

Note that not all windows are necessarily NSWindows, and that NSWindow only provides an interface to windows in your own address space.

The supported way to access every window is the CGWindow API. Take a look at the Son of Grab sample code to see how it's done.

Wednesday, June 30, 2021
 
Taptronic
answered 6 Months ago
52

I also think that emulating Ctrl-C in order to kill ssh entirely is problematic.

What I would do, is one of the following approaches. Either use windows commands to find out who are ssh's sons (which is a little bit problematic, since you need to know your current pid in order to recieve your own children-processes). I believe pstools of sysinternals is a good command-line tool that should enable you to track orphan processes. See this example for controlling windows processes either with taskList.exe (which can give you its output in CSV format BTW) or by executing a special VBScript.

The second approach is using a java library such as winp to execute and control the ssh process. I believe you'd be able to list all its children and forcibly kill them if sending the correct message would not suffice. This would be my preferred approach. Please note that the killRecursively method does exactly what you want.

Please note that those approaches should not render your application windows only. You can encapsulate those in a class that would run differently on Windows and linux machines.

Please note I didn't try to gain a fine-grained control on windows processes with, so I'm not sure how mature would those solutions I found are.

Friday, August 6, 2021
 
Yrtymd
answered 4 Months ago
76

An object is a reference towards a memory address. Then, the fields of this objects are other references towards other memory addresses. Hence, a list of objects is a list of references. So, it's impossible for the list to direclty access the object fields (references given by the references). The short answer is no.

Note: anyway you'll find an API that does what you want, it still loops in the inside.

Saturday, September 4, 2021
 
skrilled
answered 3 Months ago
43

Switch to at least the 1.18.20 version of Lombok that contains the fix

<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.18.20</version>
</dependency>
Friday, September 24, 2021
 
user505210
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share