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.
add this command line
String command="powershell -command \"get-Process cmd | format-table mainwindowtitle\"";
and use it here
Process p = Runtime.getRuntime().exec(command);
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With