How to get opened application in windows(taskmanager ->application tab content) using java code

前端 未结 2 627
面向向阳花
面向向阳花 2021-01-28 05:36

Below one is used for get list of processes from the windows

       Process proc = Runtime.getRuntime().exec (\"tasklist.exe\");

相关标签:
2条回答
  • 2021-01-28 05:49

    I am trying to give the answer, even if the post is already more than one year old.

    Try with the following code snippet:

    private static final String STR_MATCH = "My Java Application";
    String strProcess;
    BufferedReader input = null;
    
    try { 
        if(System.getProperty("os.name").startsWith("Windows")) {
            Process p = Runtime.getRuntime().exec(System.getenv("windir") + "\\system32\\" + "tasklist.exe /v /FI \"STATUS eq RUNNING\" /FI \"imagename eq javaw.exe\"");
            input = new BufferedReader(new InputStreamReader(p.getInputStream()));
    
            while((strProcess = input.readLine()) != null) {
                if(strProcess.contains(STR_MATCH))
                    /* lot of code here */
            }
    
            // Close resources
            input.close();
    }
    catch(IOEXception ioe) {
        /* handle exception */
    }
    

    The thing to notice here is the way used to get the Process p, i.e.:

    Process p = Runtime.getRuntime().exec(System.getenv("windir") + "\\system32\\" + "tasklist.exe /v /FI \"STATUS eq RUNNING\" /FI \"imagename eq javaw.exe\"");
    

    The filter /FI "STATUS eq RUNNING" /FI "imagename eq javaw.exe" that has been added when invoking tasklist.exe allows to get more information related to the process javaw.exe, including the Window/Application name.

    0 讨论(0)
  • 2021-01-28 06:02

    This is another aproach to parse the 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();
        }
    

    If you are using Windows, then you should change the line: "Process p = Runtime.getRun..." etc... (3rd line), for one that looks like this:

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

    Its not possible to get the application, because this are only the open windows shown on this tab. Every application is a process.

    U could try this from c#:

    var openWindowProcesses = System.Diagnostics.Process.GetProcesses()
       .Where(p => p.MainWindowHandle != IntPtr.Zero && p.ProcessName != "explorer");
    

    The openWindowProcesses should contains all open application which they have an active man window.

    I put 'p.ProcessName != "explorer"' in the where expression because the explorer is the main process of the Desktop and it never should be closed.

    To watching execution of the processes you can use "ManagementEventWatcher" class. See this please.

    0 讨论(0)
提交回复
热议问题