Java 9 – Process API Improvements

In this post, we will see about Java 9 process API improvements.

Java improved its Process API in Java 9 version that includes new methods for Process class and two new interfaces ProcessHandle and ProcessHandle.Info. These methods are used to create a new process and get process information like process status, running time, process id, etc. We can also get the current running process and its information.

The Process Class

Java Process class is located in java.lang package and provides methods to control the processes started by ProcessBuilder.start and Runtime.exec.

Start a new Process?

We can call start() method of ProcessBuilder class to start a new process. It will return an instance of Process class that further can be used to get process-related information.

Methods

Although Process class contains several methods but here we are listing the new methods added into Java 9 version.

MethodDescription
boolean supportsNormalTermination()It returns true if the implementation of destroy() is to normally terminate the process, else returns false.
ProcessHandle toHandle()It returns a ProcessHandle for the Process.
long pid()It returns the native process ID of the process.
Stream children()It returns a snapshot of the direct children of the process.
Stream descendants()It returns a snapshot of the descendants of the process.
ProcessHandle.Info info()It returns a snapshot of information about the process.
CompletableFuture onExit()It returns a CompletableFuture for the termination of the Process.

Example to get Process Id

After creating a process (in the above example), we are getting process id using pid() method of Process class. See the example below.

Output

Process Id: 10754

Example to get Process Information

Here is an example to get more information about the process such as hashcode, children and class.

Output

15066
[user: Optional[irfan], cmd: /usr/bin/vim.basic, startTime: Optional[2020-09-17T07:50:15.080Z], totalTime: Optional[PT0S]] 1418481495
true
java.util.stream.ReferencePipeline$2@65ab7765
class java.lang.ProcessImpl
0

ProcessHandle Interface

This interface is added into Java 9 to provide control of native or created processes. It provides additional support for process handling. We can use its current() method to get current process handler. Let’s see an example.

Output

Process Id: 17044

Methods

The following are the methods of ProcessHandle Interface.

MethodDescription
static Stream allProcesses()It returns a snapshot of all processes visible to the current process.
Stream children()It returns a snapshot of the current direct children of the process.
int compareTo(ProcessHandle other)It compares this ProcessHandle with the specified ProcessHandle for order.
static ProcessHandle current()It returns a ProcessHandle for the current process.
Stream descendants()It returns a snapshot of the descendants of the process.
boolean destroy()It requests the process to be killed.
boolean destroyForcibly()It requests the process to be killed forcibly.
boolean equals(Object other)It returns true if other object is non-null, is of the same implementation, and represents the same system process; otherwise it It returns false.
int hashCode()It returns a hash code value for this ProcessHandle.
ProcessHandle.Info info()It returns a snapshot of information about the process.
boolean isAlive()It tests whether the process represented by this ProcessHandle is alive.
static Optional of(long pid)It returns an Optional for an existing native process.
CompletableFuture onExit()It returns a CompletableFuture for the termination of the process.
Optional parent()It returns an Optional for the parent process.
long pid()It returns the native process ID of the process.
boolean supportsNormalTermination()It returns true if the implementation of destroy() normally terminates the process.

Example

We will use ProcessHandle interface to get more information of currently running process.

Output

Process Id: 12437
true
false
true
PT0.11S
irfan

ProcessHandle.Info Interface Methods

It is a nested interface of ProcessHandle interface and used to provide support for Process handling.

MethodDescription
Optional arguments()It returns an array of Strings of the arguments of the process.
Optional command()It returns the executable pathname of the process.
Optional commandLine()It returns the command line of the process.
Optional startInstant()It returns the start time of the process.
Optional totalCpuDuration()It returns the total cputime accumulated of the process.
Optional user()It returns the user of the process.

That’s all about Java 9 Process API improvements.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *