I would like to read the Active applications running in Task Manager (Applications Tab).
I have tried the code
Runtime.getRuntime().exec("tasklist.exe /nh")
which is getting the processes that are active but i need the Active applications,
I would like to read the Active applications running in Task Manager (Applications Tab).
I have tried the code
Runtime.getRuntime().exec("tasklist.exe /nh")
which is getting the processes that are active but i need the Active applications,
The active application is notepad.exe. Looks like you want/need the current title of the application. This can be done by executing wmic
program and process
command. You can do this in Java using ProcessBuilder
and Process
. Code adapted from Detect if a process is running using WMIC:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
class WindowsUtils {
private WindowsUtils() {
}
public static String listOfProcesses()
throws IOException {
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
List<String> command = new ArrayList<String>();
command.add("WMIC");
command.add("process");
try {
ProcessBuilder builder = new ProcessBuilder(command);
Process process = builder.start();
is = process.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
StringBuilder sw = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sw.append(line.trim());
sw.append(System.lineSeparator());
}
return sw.toString();
} finally {
if (br != null)
br.close();
if (isr != null)
isr.close();
if (is != null)
is.close();
}
}
}
public class WmicExample {
public static void main(String[] args) throws Exception {
System.out.println(WindowsUtils.listOfProcesses());
}
}
You can get close to what you want, but not likely exactly what you want by using creative filtering to "weed out" the processes that aren't likely in the Applications tab, but you'll still be presented with the process information.
Try this command:
tasklist /V /FI "STATUS eq RUNNING" /FI "Windowtitle ne N/A" /FI "Username eq <your username>" /FI "Memusage gt 15000"