How to get opened applications list in windows(taskmanager ->application) using java code?

不羁岁月 提交于 2019-12-01 07:37:47
soni2

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

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;

}

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!