
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How can we get an ID of the running process in Java 9?
Java 9 has added improvements to Process API for getting PID of running process, getting children and/or descendants of a process, and also added a new class that helps to list out all running processes, getting information about an arbitrary process, and traversing process tree. The information returned by these methods can be a snapshot of processes running on the OS.
In the below example, we can get an ID of the running process by using the pid() method of ProcessHandle.
Example
public class ProcessHandleTest { public static void main(String args[]) { ProcessHandle processHandle = ProcessHandle.current(); System.out.println("PID of running Process: " + processHandle.pid()); System.out.println("Command: " + processHandle.info().command().orElse("N/A")); System.out.println("CPU Duration: " + processHandle.info().totalCpuDuration().get().getSeconds() + " seconds"); } }
Output
PID of the running Process: 4248 Command: C:\Program Files\Java\jdk-9.0.4\bin\java.exe CPU Duration: 0 seconds
- Related Articles
- How can we execute snippets by ID in JShell in Java 9?
- How to get the parent process of the Process API in Java 9?
- How can we create an instance of VarHandle in Java 9?
- How can we create an unmodifiable Set in Java 9?
- How can we create an unmodifiable List in Java 9?
- How can we modify an existing module in Java 9?
- How can we change the id of an immutable string in Python?
- How to get all children of a process using Process API in Java 9?
- How can we create an unmodifiable Map in Java 9?\n
- How can we customize the start of JShell in Java 9?
- How can I get the ID of an DOM element using jQuery?
- How to get the start time of a long running Linux Process?
- How can we implement the SubmissionPublisher class in Java 9?
- How can we implement the Subscriber interface in Java 9?
- How to get a snapshot of information about Process API in Java 9?

Advertisements