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.

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.

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.

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.

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.

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.

That’s all about Java 9 Process API improvements.

Related Posts

  • 12 October

    Java 9 JShell tutorial

    The Java JShell is a command-line tool that is introduced in Java 9 version. It helps to execute/test Java statements in a single line. It is based on REPL (Read Evaluate Print Loop) that reads a code statement, execute that and print the output, statement after statement. It reads the statement and immediately shows the […]

  • 11 October

    Java 9: Stream API Improvements

    In Java 9, following methods are added to Stream interface of stream package. These methods are default and static. The takeWhile() Method This method is included in Stream interface and used to collect the elements into a stream. It takes all the elements till the condition specified as predicate. For example, we want to collect […]

  • 19 September

    Java try with resources

    In this post, we will see about Java try with resources Statement. Java try with resources is a feature of Java which was added into Java 7. It helps to close all the resources declared within try block. It automatically closes the resources after being used. A resource can be any file or a database […]

  • 19 September

    Java 9 – Javadoc Improvement

    Java has upgraded its Javadoc for better user interaction and user-experience. It includes a simplified search, HTML generator, Doclet API, etc. What is Javadoc The Javadoc is a tool like javac and a part of JDK. It is used to generate HTML documentation of Java source code. If you ever visit to oracle site you […]

  • 19 September

    Java 9 – @SafeVarargs Annotation

    In this post, we will see about @SafeVarargs Annotation in Java 9. The @SafeVarargs is an annotation that is used to perform safe operations. When a method takes variable arguments, then it may cause to unsafe operation, so the @SafeVarargs annotation tells to the compiler to perform safe operations. For example, if we don’t use […]

  • 19 September

    Java 9 – Anonymous Inner classes and Diamond Operator

    Type inference is a feature of Java that was introduced in Java 7. Now, Java compiler can infer type of a data automatically. For example, if we create an ArrayList then we use the code like: [crayon-600831df95f7b376163539/] You can see that Java 7 allows us to use empty diamond operator to avoid code redundancy. But […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.