首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用java代码在windows(任务管理器->application)中获取打开的应用程序列表?

如何使用java代码在windows(任务管理器->application)中获取打开的应用程序列表?
EN

Stack Overflow用户
提问于 2016-03-12 19:02:33
回答 4查看 3.5K关注 0票数 4

这是我非常想要的快照!

我正在尝试开发一个java程序,可以得到所有打开的任务栏中的应用程序。我尝试了许多链接,但没有一个对我有帮助。Ganesh Rangarajan in July 2013也问了同样的问题,但没有人回答他。这是他的question

EN

Stack Overflow用户

发布于 2016-05-14 20:28:29

这个回复对你来说太晚了,但可能会帮助正在面临类似问题的其他人。

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

并且您可以将listCommand替换为

代码语言:javascript
运行
复制
powershell -command "get-Process  | format-table mainwindowtitle"

(注意在java中使用它)来获取所有打开的应用程序。

代码语言:javascript
运行
复制
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;

}
票数 1
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35956816

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档