

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- How to get the parent process of the Process API in Java 9?
- How can we execute snippets by ID in JShell in Java 9?
- How can we create an instance of VarHandle in Java 9?
- How to get all children of a process using Process API 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 create an unmodifiable Map in Java 9?
- How can we modify an existing module in Java 9?
- How can we customize the start of JShell in Java 9?
- How can we change the id of an immutable string in Python?
- 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?
- How to traverse a process tree of Process API in Java 9?
Advertisements