-2

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,

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
  • possible duplicate of [How to get a list of current open windows/process with Java?](http://stackoverflow.com/questions/54686/how-to-get-a-list-of-current-open-windows-process-with-java) – Michael Barz Jul 10 '14 at 17:01
  • 3
    What do you mean by active applications? – Luiggi Mendoza Jul 10 '14 at 17:01
  • 1
    @Cid1025 OP already said he uses `tasklist /nh` – Luiggi Mendoza Jul 10 '14 at 17:02
  • @LuiggiMendoza--For Example if we use tasklist.exe /nh it will give all the processes like if i opened sample notepad like A.txt and B.txt i get as notepad.exe but i dont get the name,here in my case i need the name of the active application that is running in task manager which can be found in Applications tab – Krishna Tej Vakkalanka Jul 10 '14 at 17:14

2 Answers2

0

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());
    }
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
0

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"
Ryan J
  • 8,275
  • 3
  • 25
  • 28