Home  >  Article  >  Java  >  In Java 9, how can we get the ID of a running process?

In Java 9, how can we get the ID of a running process?

WBOY
WBOYforward
2023-08-30 21:37:02788browse

在Java 9中,我们如何获取正在运行的进程的ID?

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 the

pid() method of ProcessHandle to get the ID of the running process.

Example

public 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");
   }
}

Output

<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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete