
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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 Articles
- 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 get a snapshot of information about Process API in Java 9?
- How to terminate/destroy a process using Process API in Java 9?
- What are the improvements in Process API in Java 9?
- How to retrieve all processes data of Process API in Java 9?
- What are the new methods added to 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?
- Process vs Parent Process vs Child Process
- 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?
- How to process Arrays in Java?
- Python program to communicate between parent and child process using the pipe.

Advertisements