Home > Java > javaTutorial > body text

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

WBOY
Release: 2023-08-19 18:09:20
forward
677 people have browsed it

在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>
Copy after login

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>;
      }
   }
}
Copy after login

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>
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!