What are the new methods added to Process API in Java 9?



In this article, we will learn about the new methods added to the Process API in Java 9. First, we will know about what is process API, and the methods in Process A
PI in detail.

What is Process API?

In Java, the Process API, we can perform any operation regarding a process. If one must get the process ID of currently running process, or to create a new process, or destroy an already running one, or find child and parent processes of currently running process, or just get any information about a process, then the Process API is used to perform these operations.

New Methods in Process API

Java 9 improves Process class by adding new methods and also provides a new interface: ProcessHandle and ProcessHandle.Info to get all the details about the process and its information.

Below is the list of new methods added to Process in Java 9:

ProcessHandle Interface Methods

The following are the new methods in the ProcessHandle Interface:

long pid()

It can return the native process ID of the process.

long pid = currentProcess.pid();

ProcessHandle toHandle()

It can return a ProcessHandle for the Process.

ProcessHandle handle = process.toHandle();

boolean supportsNormalTermination()

It can return true if the implementation of the destroy() is to normally terminate the process, else it returns false.

boolean supportsTermination = current.supportsNormalTermination();

Stream children()

It can return a snapshot of the direct children of the process.

ProcessHandle.current().children()
    .forEach(child -> System.out.println(child.pid()));

Stream descendants()

It can return a snapshot of the descendants of the process.

ProcessHandle.current().descendants()
    .forEach(descendant -> System.out.println(descendant.pid()));

ProcessHandle.Info Interface Methods

The following are the new methods in the ProcessHandle.Info Interface:

ProcessHandle.Info info()

It can return a snapshot of information about the process.

ProcessHandle.Info processInfo = ProcessHandle.current().info();

user()

It returns the user of the current process.

Optional user1 = info.user();

command()

It returns the command by which the process is started.

Optional cmd = info.command();

startInstant()

It returns the time at which the current process started.

Optional start_instant = info.startInstant();

totalCpuDuration()

It returns the total CPU duration of the current process.

Optional cpu_duration = info.totalCpuDuration();

Process Control Methods

The following are the new methods in Process Control:

CompletableFuture onExit()

It can return a CompletableFuture for the termination of the process.

process.onExit()

boolean isAlive()

It returns true if the process is currently active, else it returns false.

boolean isRunning = process.isAlive();

Process destroyForcibly()

It destroys the currently active process forcibly.

process.destroyForcibly();

Example of New Methods

Below is an example of some of the most common methods of the Process API:

public class ProcessTest {
   public static void main(String args[]) {
      ProcessHandle processHandle = ProcessHandle.current();
      ProcessHandle.Info processInfo = processHandle.info();
      System.out.println(processHandle.pid());
      System.out.println(processHandle.parent());
      System.out.println(processInfo.arguments().isPresent());
      System.out.println(processInfo.command().isPresent());
      System.out.println(processInfo.command().get().contains("tutorialspoint"));
      System.out.println(processInfo.startInstant().isPresent());
   }
}

Output

4892
Optional[7788]
false
true
false
true
Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-06-09T14:27:30+05:30

176 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements