Java 9 Process API Improvement

Java has improved its process API in Java 9 version that helps to manage and control operating system processes.

In earlier versions, it was complex to manage and control operating system processes by using Java programming. Now, new classes and interfaces are added to perform this task.

New methods are added to the java.lang.Process class that are tabled below.

Modifier and Type Method Description
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.

New interfaces ProcessHandle and ProcessHandle.Info are added.


Java ProcessHandle Interface

ProcessHandle helps to handle and control processes. We can monitor processes, list its children, get information etc.

This interface contains static factory methods that return instances that are value-based, immutable and thread-safe.

Java ProcessHandle Interface Signature

This interface contains the following methods.

Modifier and Type Method Description
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.

Java ProcessHandle.Info Interface

It is added to Java 9, and used to provide information about the process. It is nested interface of ProcessHandle interface.

Java ProcessHandle.Info Interface Signature

Modifier and Type Method Description
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.

Java 9 Process API Example

Output:

Process Id: 9111
Direct children: java.util.stream.ReferencePipeline$2@6adca536
Class name: class java.lang.ProcessHandleImpl
All processes: java.util.stream.IntPipeline$1@28f67ac7
Process info: [user: Optional[w3cschoool], 
cmd: /usr/lib/jvm/java-9-oracle/bin/java, args: [-Dfile.encoding=UTF-8, 
-classpath, /home/w3cschoool/irfan/java 9/java 9 programms/Java9Features/bin, 
ProcessApiExample], startTime: Optional[2017-11-18T06:30:57.940Z], totalTime: Optional[PT0.25S]]
Is process alive: true
Process's parent: Optional[7509]





Contact US

Email:[email protected]

Process API Improvement
10/30