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

在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. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.