

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get the parent process of the Process API in Java 9?
ProcessHandle interface allows us to perform some actions, and check the state of a process. It provides the process’s native pid, start time, CPU time, user, parent process, and descendants. We can get access to a parent process by calling the parent() method, and the return value is Optional. It is empty if the child process doesn't have a parent or if the parent is not available.
Syntax
Optional<ProcessHandle> parent()
Example
import java.io.*; public class ParentProcessTest { public static void main(String args[]) { try { Process notepadProcess = new ProcessBuilder("notepad.exe").start(); ProcessHandle parentHandle = notepadProcess.toHandle().parent().get(); System.out.println("Parent Process Native PID: "+ parentHandle.pid()); } catch(IOException e) { e.printStackTrace(); } } }
In the above example, a "notepad" application will be launched, and also prints the parent process native PID.
Output
Parent Process Native PID : 7108
- Related Questions & Answers
- How to get all children of a process using Process API in Java 9?
- How to traverse a process tree of Process API in Java 9?
- How to terminate/destroy a process using Process API in Java 9?
- How to get a snapshot of information about Process API in Java 9?
- What are the improvements in Process API in Java 9?
- Process vs Parent Process vs Child Process
- What are the new methods added to Process API in Java 9?
- How to retrieve all processes data of Process API in Java 9?
- What are the core library changes in Process API in Java 9?
- How can we get an ID of the running process in Java 9?
- How to create a process using ProcessBuilder in Java 9?
- How to get all the Get-Process properties using PowerShell?
- How to get the Process performance counter using PowerShell?
- Python program to communicate between parent and child process using the pipe.
- How to process Arrays in Java?
Advertisements