In this post, we will see about Java 9 process API improvements.
Table of Contents
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.
1 2 3 |
Process process = new ProcessBuilder("vim").start(); // vim is an editor in Linux |
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.
1 2 3 4 5 6 7 8 9 10 11 |
import java.io.IOException; public class Main{ public static void main(String[] args) throws IOException { Process process = new ProcessBuilder("vim").start(); // Get process id System.out.println(process.pid()); } } |
Output
Example to get Process Information
Here is an example to get more information about the process such as hashcode, children and class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.io.IOException; public class Main{ public static void main(String[] args) throws IOException { Process process = new ProcessBuilder("vim").start(); // Get created process info System.out.println(process.pid()); System.out.println(process.info()); System.out.println(process.hashCode()); System.out.println(process.isAlive()); System.out.println(process.children()); System.out.println(process.getClass()); System.out.println(process.descendants().count()); } } |
Output
[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.
1 2 3 4 5 6 7 8 |
public class Main{ public static void main(String[] args) { ProcessHandle processHandle = ProcessHandle.current(); // current process System.out.println("Process Id: "+processHandle.pid()); } } |
Output
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class Main{ public static void main(String[] args) { ProcessHandle processHandle = ProcessHandle.current(); System.out.println("Get Process Info: "); System.out.println("Process Id: "+processHandle.pid()); ProcessHandle.Info pHandleInfo = processHandle.info(); System.out.println(pHandleInfo.arguments().isPresent()); System.out.println(pHandleInfo.arguments().isEmpty()); System.out.println(pHandleInfo.command().isPresent()); System.out.println(pHandleInfo.totalCpuDuration().get()); // process user name System.out.println(pHandleInfo.user().get()); } } |
Output
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.