简体   繁体   中英

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?

I can do so in Windows using the code below to return the Task List, but that throws an exception on a Mac. I want my app to stop if certain applications are also running.

Any ideas? Thanks.

Windows Code:

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. Use something like ps -eaf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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