Home  >  Article  >  Java  >  How to get the parent process of process API in Java 9?

How to get the parent process of process API in Java 9?

WBOY
WBOYforward
2023-08-19 18:09:20677browse

在Java 9中如何获取进程API的父进程?

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.

Syntax

<strong>Optional<ProcessHandle> parent()</strong>

Example

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.

Output

<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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete