Home >Java >javaTutorial >How to get the parent process of process API in Java 9?
ProcessHandle Interface allows us to perform some operations and check the status of the process. It provides the process's native pid, start time, CPU time, user, parent process and descendants. We can access the parent process by calling the parent() method, and the return value is Optional. If the child process has no parent process or the parent process is unavailable, the return value is empty.
<strong>Optional<ProcessHandle> parent()</strong>
import java.io.*; public class ParentProcessTest { public static void main(String args[]) { try { <strong>Process </strong>notepadProcess = new <strong>ProcessBuilder</strong>("notepad.exe").start(); <strong>ProcessHandle </strong>parentHandle = notepadProcess.<strong>toHandle()</strong>.<strong>parent()</strong>.get(); System.out.println("Parent Process Native PID: "+ parentHandle.<strong>pid</strong>()); } catch(IOException e) { e.<strong>printStackTrace()</strong>; } } }
In the above example, a "Notepad" application will be started and the parent process will be printed out. Local PID.
<strong>Parent Process Native PID : 7108</strong>
The above is the detailed content of How to get the parent process of process API in Java 9?. For more information, please follow other related articles on the PHP Chinese website!