Get a list of active programs in java

Parzavil :

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.

Khalil M :

add this command line

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

and use it here

Process p = Runtime.getRuntime().exec(command);

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=457867&siteId=1