4

Here is the snapshot what I want exately!

Windows Application Task List

I am trying to develop a program in java which can get all opened application in the taskbar. I have tried many links but none of those are helpful to me. The same question was also asked by Ganesh Rangarajan in July 2013 but none has answered him. Here is his question.

Community
  • 1
  • 1

4 Answers4

4

Here is the solution to get titles of ALL (visible, non-visible) windows: https://stackoverflow.com/a/11067492/6401177

If you want to get titles of opened top-level windows only (i.e. Applications taskbar), you have to check the visibility of each window (and/or check other conditions as listed here: http://vb.mvps.org/articles/ap200003.asp). Although, checking window's visibility seems sufficient.

I just altered method "callback" in previous code like this:

String wText = Native.toString(windowText, System.getProperty("file.encoding")).trim();
        com.sun.jna.platform.win32.WinDef.HWND hwnd_1 = new WinDef.HWND(hWnd);
        boolean b = com.sun.jna.platform.win32.User32.INSTANCE.IsWindowVisible(hwnd_1);
        if (!wText.isEmpty() && b) {
           windowNames.add(wText);
        }

I also added "file.encoding" so titles are shown correctly in non-english Windows environment too. I tested code in Windows XP/7/8 and it works nice. The only problem seems to be that some default internal(?) window called "Program Manager" is always included in the list.

You need both JARs (JNA libraries) from: https://github.com/java-native-access/jna

Community
  • 1
  • 1
soni2
  • 41
  • 2
  • good work now it is fetching the all names under applications, is their any way get services and processes – Narasimha Apr 10 '20 at 16:24
1

This reply is too late for you, but may help someone else who is now facing a similar issue.

I just wrote a similar applications to do it but to opened CMD only

and you can replace the listCommand by

powershell -command "get-Process  | format-table mainwindowtitle"

(takig care with \ to use it in java) to get all opened applications .

public String[] getEnginesFromTaskManger()
{


    // listCommand is a command to get all opened CMD program like (batches,.......) 
    // you can test it in your CMD as powershell -command "get-Process cmd | format-table mainwindowtitle"
    String listCommand = "powershell -command \"get-Process cmd | format-table mainwindowtitle\"";
    try
    {
        String line;


        // since line length for powershell output is 79
        int outLen = 79;



        Process p = Runtime.getRuntime().exec(listCommand);
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        line = input.readLine();
        System.out.println("line: " + line + "\t" + line.length());



        EnginesListFromTaskManeger = new ArrayList<String>();
        int i = 0;

        /*
         I used this outLen > 0 condition to make sure that this method will close automatically 
         in case of no running CMD applications and you running this from your IDE's (Eclipse, Netbeans , ......) 
         the powershell will not stopped so i used it. */
        while(line != null && outLen > 0)
        {
            System.out.println("line: " + line + "\t" + line.length());

            line = input.readLine().trim().toLowerCase();
            outLen = line.length();

            EnginesListFromTaskManeger.add(i, line);

            System.out.println(EnginesListFromTaskManeger.get(i));
            // EnginesListFromTaskManeger[i]=(String)input.readLine().trim();
            // System.out.println("EnginesListFromTaskManeger"+ EnginesListFromTaskManeger[i]);
            i++;
        }
        input.close();
    }catch(Exception err)
    {
        err.printStackTrace();
    }

    ListFromTaskManeger = new String[EnginesListFromTaskManeger.size()];
    ListFromTaskManeger = EnginesListFromTaskManeger.toArray(ListFromTaskManeger);

    return ListFromTaskManeger;

}
Nothig
  • 39
  • 9
0

The process list from the command "ps -e":

try {
      String line;
      Process p = Runtime.getRuntime().exec("ps -e");
      BufferedReader input =
      new BufferedReader(new InputStreamReader(p.getInputStream()));
      while ((line = input.readLine()) != null) {
             System.out.println(line); //<-- Parse data here.
       }
      input.close();
    } catch (Exception err) {
            err.printStackTrace();
    }

In windows see following used following code

Process p = Runtime.getRuntime().exec
(System.getenv("windir") +"\\system32\\"+"tasklist.exe");

Hope the info help!

Sanjay Bhalani
  • 2,424
  • 18
  • 44
0

"tasklist.exe /nh /v" worked perfectly to get the running applications.

    try {
            Process p = Runtime.getRuntime().exec("tasklist.exe /nh /v");
            BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
            while ((String line = input.readLine()) != null) {
                System.out.println(line); //<-- Parse data here.
            }
            input.close();
        } catch (Exception err) {
            err.printStackTrace();
        }
Jawad
  • 11,028
  • 3
  • 24
  • 37