
- 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 to retrieve all processes data of Process API in Java 9?
In Java 9, Process API has been used to control and manage operating system processes. ProcessHandle class provides the process’s native process ID, start time, accumulated CPU time, arguments, command, user, parent process, and descendants. It also provides a method to check processes liveness and to destroy processes. We retrieve all ProcessHandle data as a stream by using the allProcesses() method.
In the below example, we retrieve all the processes information.
Example
import java.util.stream.Stream; import java.util.Optional; import java.util.stream.Stream; public class AllProcessesTest { public static void main(String args[]) throws InterruptedException { System.out.println("---------------------------"); System.out.println("All Processes:"); Stream<ProecessHandle> processStream = ProcessHandle.allProcesses(); processStream.forEach(process -> printInfo(process)); } private static void printInfo(ProcessHandle processHandle) { System.out.println("---------"); System.out.println("Id: " + processHandle.pid()); System.out.println("isAlive(): " + processHandle.isAlive()); System.out.println("number of childrens: " + processHandle.children().count()); System.out.println("isSupportsNormalTermination(): " + processHandle.supportsNormalTermination()); ProcessHandle.Info processInfo = processHandle.info(); System.out.println("Info: " + processInfo.toString()); System.out.println("Info arguments().isPresent(): " + processInfo.arguments().isPresent()); System.out.println("Info command().isPresent(): " + processInfo.command().isPresent()); System.out.println("Info totalCpuDuration().isPresent(): " + processInfo.totalCpuDuration().isPresent()); System.out.println("Info user().isPresent(): " + processInfo.user().isPresent()); } }
Output
--------------------------- All Processes: Id: 7056 isAlive(): true number of childrens: 0 isSupportsNormalTermination(): false Info: [user: Optional[Tutorialspoint\User], cmd: C:\Program Files (x86)\Google\Chrome\Application\chrome.exe, startTime: Optional[2020-03-09T03:26:00.406Z], totalTime: Optional[PT1M52.15625S]] Info arguments().isPresent(): false Info command().isPresent(): true Info totalCpuDuration().isPresent(): true Info user().isPresent(): true --------- Id: 6168 isAlive(): true number of childrens: 0 isSupportsNormalTermination(): false Info: [user: Optional[Tutorialspoint\User], cmd: C:\Program Files (x86)\Google\Chrome\Application\chrome.exe, startTime: Optional[2020-03-09T03:26:01.567Z], totalTime: Optional[PT2M24.671875S]] Info arguments().isPresent(): false Info command().isPresent(): true Info totalCpuDuration().isPresent(): true Info user().isPresent(): true --------- Id: 7972 isAlive(): true number of childrens: 0 isSupportsNormalTermination(): false Info: [user: Optional[Tutorialspoint\User], cmd: C:\Program Files (x86)\Google\Chrome\Application\chrome.exe, startTime: Optional[2020-03-09T03:26:03.118Z], totalTime: Optional[PT29.09375S]] Info arguments().isPresent(): false Info command().isPresent(): true Info totalCpuDuration().isPresent(): true Info user().isPresent(): true --------- Id: 3368 isAlive(): true number of childrens: 0 isSupportsNormalTermination(): false Info: [user: Optional[Tutorialspoint\User], cmd: C:\Program Files (x86)\Google\Chrome\Application\chrome.exe, startTime: Optional[2020-03-09T03:27:26.511Z], totalTime: Optional[PT37.84375S]] Info arguments().isPresent(): false Info command().isPresent(): true Info totalCpuDuration().isPresent(): true Info user().isPresent(): true --------- Id: 2456 isAlive(): true number of childrens: 0 isSupportsNormalTermination(): false Info: [user: Optional[Tutorialspoint\User], cmd: C:\WINDOWS\System32\conhost.exe, startTime: Optional[2020-03-09T03:30:49.514Z], totalTime: Optional[PT0.390625S]] Info arguments().isPresent(): false Info command().isPresent(): true Info totalCpuDuration().isPresent(): true Info user().isPresent(): true --------- Id: 7804 isAlive(): true number of childrens: 4 isSupportsNormalTermination(): false Info: [user: Optional[Tutorialspoint\User], cmd: C:\Program Files\Mozilla Firefox\firefox.exe, startTime: Optional[2020-03-09T03:30:51.441Z], totalTime: Optional[PT38.046875S]] Info arguments().isPresent(): false Info command().isPresent(): true Info totalCpuDuration().isPresent(): true Info user().isPresent(): true --------- Id: 8172 isAlive(): true number of childrens: 0 isSupportsNormalTermination(): false Info: [user: Optional[Tutorialspoint\User], cmd: C:\Program Files\Mozilla Firefox\firefox.exe, startTime: Optional[2020-03-09T03:30:53.293Z], totalTime: Optional[PT4.03125S]] Info arguments().isPresent(): false Info command().isPresent(): true Info totalCpuDuration().isPresent(): true Info user().isPresent(): true --------- Id: 6008 isAlive(): true number of childrens: 0 isSupportsNormalTermination(): false Info: [user: Optional[Tutorialspoint\User], cmd: C:\Program Files\Mozilla Firefox\firefox.exe, startTime: Optional[2020-03-09T03:30:54.081Z], totalTime: Optional[PT5M28.078125S]] Info arguments().isPresent(): false Info command().isPresent(): true Info totalCpuDuration().isPresent(): true Info user().isPresent(): true --------- Id: 1032 isAlive(): false number of childrens: 0 isSupportsNormalTermination(): false Info: [] Info arguments().isPresent(): false Info command().isPresent(): false Info totalCpuDuration().isPresent(): false Info user().isPresent(): false --------- Id: 5044 isAlive(): true number of childrens: 0 isSupportsNormalTermination(): false Info: [user: Optional[Tutorialspoint\User], cmd: C:\Program Files\Java\jdk-9.0.4\bin\java.exe, startTime: Optional[2020-03-09T04:12:39.567Z], totalTime: Optional[PT1.28125S]] Info arguments().isPresent(): false Info command().isPresent(): true Info totalCpuDuration().isPresent(): true Info user().isPresent(): true
- Related Articles
- How to get all children of a process using Process API in Java 9?
- How to traverse a process tree of Process API in Java 9?
- How to get the parent process of the Process API in Java 9?
- How to terminate/destroy a process using Process API in Java 9?
- How to get a snapshot of information about Process API in Java 9?
- How to print all attributes in StackFrame API in Java 9?
- What are the improvements in Process API in Java 9?
- What are the new methods added to Process API in Java 9?
- What are the core library changes in Process API in Java 9?
- How to retrieve android API version programmatically?
- StackWalker API in Java 9?
- How to implement reactive streams using Flow API in Java 9?
- How to filter stack frames using StackWalker API in Java 9?
- How can we implement methods of Stream API in Java 9?
- How to retrieve Android API version programmatically using Kotlin?

Advertisements