Home > Java > javaTutorial > body text

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

WBOY
Release: 2023-08-30 21:37:02
forward
718 people have browsed it

在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");
   }
}
Copy after login

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>
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!