下面的一个用于从窗口获取进程列表
Process proc = Runtime.getRuntime().exec ("tasklist.exe");
但我想获得应用程序选项卡内容本身,我需要一个解决scheme
Process proc = Runtime.getRuntime().exec ( ? ? ?);
这是从命令“ 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(); }
如果你正在使用Windows,那么你应该改变一行:“Process p = Runtime.getRun …”etc …(第三行),看起来像这样:
Process p = Runtime.getRuntime().exec (System.getenv("windir") +"\\system32\\"+"tasklist.exe");
它不可能得到应用程序,因为这只是在此选项卡上显示的打开的窗口。 每个应用程序是一个过程。
你可以试试这个从C#中:
var openWindowProcesses = System.Diagnostics.Process.GetProcesses() .Where(p => p.MainWindowHandle != IntPtr.Zero && p.ProcessName != "explorer");
openWindowProcesses应该包含所有打开的应用程序,它们有一个活动的人窗口。
我把'p.ProcessName!=“explorer”'放在where表达式中,因为资源管理器是桌面的主进程,它永远不会关闭。
要监视进程的执行情况,您可以使用“ManagementEventWatcher”类。 请看这个 。
我试图给出答案,即使这个职位已经有一年多了。
尝试使用以下代码片段:
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 */ }
这里要注意的是用来获得Process p
,即:
Process p = Runtime.getRuntime().exec(System.getenv("windir") + "\\system32\\" + "tasklist.exe /v /FI \"STATUS eq RUNNING\" /FI \"imagename eq javaw.exe\"");
在调用tasklist.exe
时添加的过滤器/FI "STATUS eq RUNNING" /FI "imagename eq javaw.exe"
允许获取与javaw.exe
进程相关的更多信息,包括窗口/应用程序名称。