简体   繁体   English

通过Java获取活动监视器(Mac OSX)

[英]Get Activity Monitor (Mac OSX) through Java

Is there a way using Java that I can gain a list of all active processes running on a Mac? 有没有一种使用Java的方法可以获取Mac上运行的所有活动进程的列表?

I can do so in Windows using the code below to return the Task List, but that throws an exception on a Mac. 我可以在Windows中使用以下代码返回“任务列表”,但是在Mac上会引发异常。 I want my app to stop if certain applications are also running. 如果某些应用程序也正在运行,我希望我的应用程序停止运行。

Any ideas? 有任何想法吗? Thanks. 谢谢。

Windows Code: Windows代码:

Process p = Runtime.getRuntime().exec("tasklist.exe /nh");
            BufferedReader input = new BufferedReader
            (new InputStreamReader(p.getInputStream()));

            //while there are more processes in the task manager list
            while ((line = input.readLine()) != null) {
                      //insert code here for each task running
            }
  String line;
  String sysUserName=System.getProperty("user.name");
    Process p = Runtime.getRuntime().exec("tasklist /fi  \"username      eq"+sysUserName+"\""); // for windows
    // Process p = Runtime.getRuntime().exec("ps -u "+sysUserName+""); // for mac
    BufferedReader input =
            new BufferedReader(new InputStreamReader(p.getInputStream()));
    while ((line = input.readLine()) != null) {
        System.out.println(line); //<-- Parse data here.

    }
    input.close();

tasklist.exe does not exist on Mac. 在Mac上不存在tasklist.exe。 Use something like ps -eaf 使用类似ps -eaf的东西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM