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

后端 未结 4 2074
执笔经年
执笔经年 2020-12-19 17:14

Here is the snapshot what I want exately!

\"Windows

I am trying to develop a pr

相关标签:
4条回答
  • 2020-12-19 17:25

    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;
    
    }
    
    0 讨论(0)
  • 2020-12-19 17:28

    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!

    0 讨论(0)
  • 2020-12-19 17:42

    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

    0 讨论(0)
  • 2020-12-19 17:47

    "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();
            }
    
    0 讨论(0)
提交回复
热议问题