Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a list of active programs in java

I need to retrieve a list of currently open programs using java. The following code gives me a list of all the programs that are active including any background processes however I need only a list of active programs.

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

I am not going to be aware what programs are currently open and so will not be able to find it by searching for a series of names, as I have seen some people recommend this method.

By active program I am meaning any program that is available to the user to interact with through a window. The task manager window already splits the programs(in detailed view) into apps and background processes and I would like to be able to retrieve any programs that would be sorted under the apps section.

like image 448
Parzavil Avatar asked Apr 23 '18 11:04

Parzavil


2 Answers

add this command line

String command="powershell -command \"get-Process cmd | format-table mainwindowtitle\"";

and use it here

Process p = Runtime.getRuntime().exec(command);
like image 191
Khalil M Avatar answered Oct 20 '22 20:10

Khalil M


You can use the windows cmd like this :

try {
            String process;
            // getRuntime: Returns the runtime object associated with the current Java application.
            // exec: Executes the specified string command in a separate process.
            Process p = Runtime.getRuntime().exec(System.getenv("windir") +"\\system32\\"+"tasklist.exe /fo csv /nh");
            BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
            while ((process = input.readLine()) != null) {
                System.out.println(process); // <-- Print all Process here line
                                                // by line
            }
            input.close();
        } catch (Exception err) {
            err.printStackTrace();
        }
like image 42
Korteby Farouk Avatar answered Oct 20 '22 19:10

Korteby Farouk