![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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
Advertisements