Java 9 has improved the Process API to obtain the PID## of a running process #, Get the child processes and/or descendant processes of a process, and also add a new help to list all running processes , get about Information about any process and classes for traversing the process tree. The information returned by these methods can be a snapshot of the processes running on the operating system.
In the following example, we can use thepid() method of ProcessHandle to get the ID of the running process.
Examplepublic class ProcessHandleTest { public static void main(String args[]) { <strong>ProcessHandle </strong>processHandle = <strong>ProcessHandle.current()</strong>; System.out.println("PID of running Process: " + <strong>processHandle</strong>.<strong>pid()</strong>); System.out.println("Command: " + <strong>processHandle.info()</strong>.<strong>command()</strong>.orElse("N/A")); System.out.println("CPU Duration: " + <strong>processHandle.info()</strong>.<strong>totalCpuDuration().get().getSeconds()</strong> + " seconds"); } }
<strong>PID of the running Process: 4248 Command: C:\Program Files\Java\jdk-9.0.4\bin\java.exe CPU Duration: 0 seconds</strong>
The above is the detailed content of In Java 9, how can we get the ID of a running process?. For more information, please follow other related articles on the PHP Chinese website!