How to Get the List of Running Processes in Mac Using Java

In a Mac machine, the list of all running processes can be retrieved using the following command in the console. The option -e displays all processes including processes owned by other users. The -o command flag instructs ps to show only the command name (with arguments). To get the command name without arguments, use comm instead of command.

ps -e -o command

How to List All Running Processes in Mac Using Java

Java API has a class called Runtime which can be used to run operating system commands such as ps in Mac.  The following sample program in Java demonstrates the use of Runtime class to get a list of running processes from the Mac machine.

import java.io.BufferedReader;
import java.io.InputStreamReader;

// Displays all running processes in a Mac machine.
public class ShowProcessesInMac {

    public static void main(String[] args) throws Exception {
        printAllProcesses();
    }
    // Java example program to display the names of all running mac processes
    private static void printAllProcesses() throws Exception{
        // -e - show all processes including processes of other users
        // -o command - restrict the output to just the process name
        Process process = Runtime.getRuntime().exec("ps -e -o command");
        BufferedReader r =  new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = null;
        
        while((line=r.readLine())!=null) {
            System.out.println(line);
        }
    }
}

You can use the variations of the ps command to get different types of data regarding running processes.

ps -er -o %cpu,command

The above command displays all processes sorted in the descending order of cpu percentage. The columns displayed are %cpu and the process command including the command arguments.

ps -em -o %mem,command

The above command displays all processes sorted in the descending order of memory percentage. The columns displayed are %memory usage in percentage and the process command including the command arguments.

The following sample Java program shows how one of the variations above can be used to print the Mac process with the highest CPU usage. This program extracts the CPU usage and the name of the process (including process arguments) with highest CPU usage. It is quite possible that this program itself may be reported as the top CPU process!

import java.io.BufferedReader;
import java.io.InputStreamReader;

// Sample Java program to print highest CPU process in a Mac machine
// Note that this program itself may be reported as the top process!
public class TopCPUMacProcess {

    public static void main(String[] args) throws Exception {
        printProcessWithHighestCPU();
    }

    // Java example program to print the name of the process with highest CPU usage in mac
    private static void printProcessWithHighestCPU() throws Exception {
        Process process = Runtime.getRuntime().exec("ps -er -o %cpu,command");
        BufferedReader r =  new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = null;
        int rowCount = 1;
        while((line=r.readLine())!=null) {
            if(rowCount==2) break; //first row is header
            rowCount++;
        }
        line = line.trim(); // remove spaces in the beginning
        int firstSpacePos = line.indexOf(" ");
        String cpuUsage = line.substring(0, firstSpacePos);
        String processName = line.substring(firstSpacePos+1);
        System.out.println("Process with highest CPU="+processName);
        System.out.println("CPU%="+cpuUsage);
    }
}

The following example program in Java shows the top 5 processes with the highest memory usage on the Mac system,

import java.io.BufferedReader;
import java.io.InputStreamReader;

// Displays the top 5 processes with the highest memory usage in Mac
public class Top5MemoryProcessesInMac {
    public static void main(String[] args) throws Exception {
        printTop5MemoryProcesses();
    }
    
    // Java method to print top 5 processes with highest memory usage in Mac
    private static void printTop5MemoryProcesses() throws Exception {
        Process process = Runtime.getRuntime().exec("ps -em -o %mem,command");
        BufferedReader r =  new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = null;
        int rowCount = 0;
        while((line=r.readLine())!=null) {
            System.out.println(line);
            rowCount++;
            if(rowCount>5) break; // note that first line is the header
        }
    }
}

The following example program in Java prints a verbose report of all processes running in the Mac system,

import java.io.BufferedReader;
import java.io.InputStreamReader;

// Displays a verbose report of all running processes in a Mac.
public class VerboseProcessReportInMac {
    public static void main(String[] args) throws Exception {
        printVerboseProcessReport();
    }
    
    // Java program to print verbose process information.
    private static void printVerboseProcessReport() throws Exception{
        Process process = Runtime.getRuntime().exec("ps -er -o %cpu,%mem,flags,lim,lstart,nice,rss,start,state,tt,wchan,command ");
        BufferedReader r =  new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = null;
        
        while((line=r.readLine())!=null) {
            System.out.println(line);
        }
    }
}

Additional References