如何使用java代码在windows(taskmanager – >应用程序)中打开应用程序列表?

这是我想要的快照!

Windows应用程序任务列表

我正在尝试开发一个程序,可以在任务栏中获取所有打开的应用程序。 我已经尝试了很多链接,但没有一个对我有帮助。 Ganesh Rangarajan在2013年7月也提出了同样的问题,但没有人回答他。 这是他的问题 。

这里是解决所有(可见,不可见)窗口的标题: https : //stackoverflow.com/a/11067492/6401177

如果你只想得到打开的顶层窗口的标题(即应用程序任务栏),你必须检查每个窗口的可见性(和/或检查其他条件列在这里: http : //vb.mvps.org/articles /ap200003.asp )。 虽然,检查窗口的可见性似乎是足够的。

我刚刚在前面的代码中修改了方法“callback”,如下所示:

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); } 

我还添加了“file.encoding”,因此标题在非英语Windows环境中也能正确显示。 我测试了Windows XP / 7/8中的代码,它工作的很好。 唯一的问题似乎是一些名为“程序管理器”的默认内部(?)窗口总是包含在列表中。

您需要两个JAR(JNA库): https : //github.com/java-native-access/jna

这个回复对你来说已经太迟了,但是可能会帮助那些现在面临类似问题的人。

我只是写了一个类似的应用程序来做,但只是打开CMD

你可以通过替换listCommand

 powershell -command "get-Process | format-table mainwindowtitle" 

(takig关心\在java中使用它)来获取所有打开的应用程序。

 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; } 

来自命令“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.getRuntime().exec (System.getenv("windir") +"\\system32\\"+"tasklist.exe"); 

希望信息帮助!