
- 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 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 Articles
- 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 to process Arrays in Java?
- How can we get an ID of the running process in Java 9?
- StackWalker API in Java 9?
- Explain the process of lightning striking a building or tree.
- How to print all attributes in StackFrame API in Java 9?
- How to filter stack frames using StackWalker API in Java 9?

Advertisements