How to get all children of a process using Process API in Java 9?



In this article, we will learn to get all children of a process using Process API in Java 9. First, we will learn about Process API and its methods, ProcessHandle interface and after that we will use the children method of the ProcessHandle interface.

Process API

In Java, the Process API, we can perform any operation regarding a process. The Process class provides methods for performing input on the processes, performing output to the processes, waiting for the process to complete, finding child and parent processes of the currently running process, getting any information about a process, and destroying the process.

The following are some of the common methods of the Process API:

  • destroy(): This method kills the process.
  • getInputStream(): It returns the input stream connected to the normal output of the process.
  • getOutputStream(): It returns the output stream connected to the normal input of the process.
  • pid(): This method returns the native process ID of the process.

ProcessHandle Interface

The ProcessHandle interface was introduced in Java 9. It allows us to perform actions and check the state of a process that relates. This interface provides the process's native process ID (pid), start time, accumulated CPU time, user, parent process, and descendants.

Getting all the children of a Process

We can use the ProcessHandle interface and related methods to get the pid and information about other related processes. We need to get all children of a process, then use java.lang.ProcessHandle.children() method. This method returns a stream, typically a process that has no children.

The children() method comes under the ProcessHandle interface and is used to return a snapshot of the direct children of the process.

Here is the syntax:

ProcessHandle.current().children()
    .forEach(child -> System.out.println(child.pid()));

The above prints all the PIDs of the current process present in the session.

First, we will be going to define a ProcessHandle named "processHandle", in which the allProcesses() method returns a Stream of all processes visible in the current session. Then, the findFirst() method gets the first process from the stream of processes.

The ifPresent() method only executes the lambda expression if a process handle exists in the session. The children() method returns a Stream of direct child processes.

Then, we iterate through each child process and print its info and PID using the info() method and child.pid() respectively.

Code to Get All Children of a Process Using Process API

Below is the Java code to get the first process and retrieve its children's process information.

import java.util.stream.Stream;
import java.util.Optional;

public class ChilderenProcessTest {
   public static void main(String args[]) throws InterruptedException {
      System.out.println("---------------------------");
      System.out.println("Children Processes:");
      Optional<ProcessHandle> processHandle = ProcessHandle.allProcesses().findFirst();
      processHandle.ifPresent(proc -> proc.children().forEach(child -> System.out.println("PID: [ " + child.pid() + " ], Cmd: [ " + child.info().command() + " ]")));
   }
}

Output

---------------------------
Children Processes:
PID: [ 0 ], Cmd: [ Optional.empty ]
PID: [ 4 ], Cmd: [ Optional.empty ]
PID: [ 424 ], Cmd: [ Optional.empty ]
PID: [ 504 ], Cmd: [ Optional.empty ]
PID: [ 560 ], Cmd: [ Optional.empty ]
PID: [ 444 ], Cmd: [ Optional.empty ]
PID: [ 1236 ], Cmd: [ Optional.empty ]
PID: [ 1288 ], Cmd: [ Optional.empty ]
PID: [ 1408 ], Cmd: [ Optional.empty ]
PID: [ 1424 ], Cmd: [ Optional.empty ]
PID: [ 1452 ], Cmd: [ Optional.empty ]
PID: [ 1468 ], Cmd: [ Optional.empty ]
PID: [ 5412 ], Cmd: [ Optional[C:\WINDOWS\System32\taskhostex.exe] ]
PID: [ 3760 ], Cmd: [ Optional[C:\Program Files\Synaptics\SynTP\SynTPEnh.exe] ]
PID: [ 5216 ], Cmd: [ Optional[C:\WINDOWS\explorer.exe] ]
PID: [ 2460 ], Cmd: [ Optional[C:\Program Files (x86)\Dell Wireless\Bluetooth Suite\BtvStack.exe] ]
PID: [ 6064 ], Cmd: [ Optional[C:\Program Files\Realtek\Audio\HDA\RtkNGUI64.exe] ]
PID: [ 7172 ], Cmd: [ Optional[C:\Program Files (x86)\Google\Chrome\Application\chrome.exe] ]
PID: [ 860 ], Cmd: [ Optional[C:\Program Files (x86)\Google\Chrome\Application\chrome.exe] ]
PID: [ 9000 ], Cmd: [ Optional.empty ]
PID: [ 4180 ], Cmd: [ Optional[C:\WINDOWS\System32\cmd.exe] ]
PID: [ 3748 ], Cmd: [ Optional[C:\WINDOWS\System32\conhost.exe] ]
PID: [ 3376 ], Cmd: [ Optional.empty ]
PID: [ 2548 ], Cmd: [ Optional.empty ]
PID: [ 1820 ], Cmd: [ Optional[C:\Program Files\Java\jdk-9.0.4\bin\java.exe] ]
Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-06-12T15:53:54+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started