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 TypeMethodDescription
booleansupportsNormalTermination()It returns true if the implementation of destroy() is to normally terminate the process, else returns false.
ProcessHandletoHandle()It returns a ProcessHandle for the Process.
longpid()It returns the native process ID of the process.
Stream<ProcessHandle>children()It returns a snapshot of the direct children of the process.
Stream<ProcessHandle>descendants()It returns a snapshot of the descendants of the process.
ProcessHandle.Infoinfo()It returns a snapshot of information about the process.
CompletableFuture<Process>onExit()It returns a CompletableFuture<Process> 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 TypeMethodDescription
static Stream<ProcessHandle>allProcesses()It returns a snapshot of all processes visible to the current process.
Stream<ProcessHandle>children()It returns a snapshot of the current direct children of the process.
intcompareTo(ProcessHandle other)It compares this ProcessHandle with the specified ProcessHandle for order.
static ProcessHandlecurrent()It returns a ProcessHandle for the current process.
Stream<ProcessHandle>descendants()It returns a snapshot of the descendants of the process.
booleandestroy()It requests the process to be killed.
booleandestroyForcibly()It requests the process to be killed forcibly.
booleanequals(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.
inthashCode()It returns a hash code value for this ProcessHandle.
ProcessHandle.Infoinfo()It returns a snapshot of information about the process.
booleanisAlive()It tests whether the process represented by this ProcessHandle is alive.
static Optional<ProcessHandle>of(long pid)It returns an Optional<ProcessHandle> for an existing native process.
CompletableFuture<ProcessHandle>onExit()It returns a CompletableFuture<ProcessHandle> for the termination of the process.
Optional<ProcessHandle>parent()It returns an Optional<ProcessHandle> for the parent process.
longpid()It returns the native process ID of the process.
booleansupportsNormalTermination()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 TypeMethodDescription
Optional<String[]>arguments()It returns an array of Strings of the arguments of the process.
Optional<String>command()It returns the executable pathname of the process.
Optional<String>commandLine()It returns the command line of the process.
Optional<Instant>startInstant()It returns the start time of the process.
Optional<Duration>totalCpuDuration()It returns the total cputime accumulated of the process.
Optional<String>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[javatpoint], 
cmd: /usr/lib/jvm/java-9-oracle/bin/java, args: [-Dfile.encoding=UTF-8, 
-classpath, /home/javatpoint/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]





Latest Courses