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

前端 未结 2 626
面向向阳花
面向向阳花 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 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.

提交回复
热议问题