Java examples for Native OS:OS
ProcessHandle interface can obtain information regarding operating system processes.
import java.nio.file.Paths; import java.time.Duration; import java.util.Optional; public class Main { public static void main (String[] args){ ProcessHandle.allProcesses() .forEach(System.out::println); ProcessHandle.allProcesses() .forEach(h->System.out.println(formattedProcess(h))); ProcessHandle.allProcesses() .forEach(h->System.out.println(listOsUser(h))); } public static ProcessHandle.Info obtainInfo(ProcessHandle handle){ return handle.info(); } public static String formattedProcess(ProcessHandle handle){ long pid = handle.getPid(); boolean alive = handle.isAlive(); Optional<Duration> cpuDuration = handle.info().totalCpuDuration(); Optional<String> handleName = handle.info().command(); return pid + " " + alive + " " + handleName + ":"+ cpuDuration; } public static String listOsUser(ProcessHandle handle){ ProcessHandle.Info procInfo = handle.info(); return handle.getPid() + ": " +procInfo.user(); } }