

- 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 to traverse a process tree of Process API in Java 9?
Java 9 has improved Process API, and it helps to manage and control operating system processes. Before Java 9, it has been difficult to manage and control operating system processes using Java programs. Since Java 9, new classes and interfaces have added to control the operating system process through Java programs. New interfaces like ProcessHandle and ProcessHandle.Info have added, and also new methods have added to Process class.
In the below example, we can traverse a process tree (children and descendant processes) of Process API.
Example
import java.io.IOException; public class ProcessTreeTest { public static void main(String args[]) throws IOException { Runtime.getRuntime().exec("cmd"); System.out.println("Showing children processes:"); ProcessHandle processHandle = ProcessHandle.current(); processHandle.children().forEach(childProcess -> System.out.println("PID: " + childProcess.pid() + " Command: " + childProcess.info().command().get())); System.out.println("Showing descendant processes:"); processHandle.descendants().forEach(descendantProcess -> System.out.println("PID: " + descendantProcess.pid() + " Command: " + descendantProcess.info().command().get())); } }
Output
Showing children processes: PID: 5092 Command: C:\WINDOWS\System32\cmd.exe Showing descendant processes: PID: 5092 Command: C:\WINDOWS\System32\cmd.exe PID: 2256 Command: C:\WINDOWS\System32\conhost.exe
- Related Questions & Answers
- How to terminate/destroy a process using Process API in Java 9?
- How to get all children of a process using Process API in Java 9?
- How to get the parent process of the Process API in Java 9?
- How to get a snapshot of information about Process API in Java 9?
- How to retrieve all processes data of Process 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 create a process using ProcessBuilder in Java 9?
- How can we get an ID of the running process in Java 9?
- How to process Arrays in Java?
- Process vs Parent Process vs Child Process
- Resuming Process Monitoring for a Process Instance
- StackWalker API in Java 9?
- How to create a process in Linux?
Advertisements