Java 9 Process API Introduction

Java 9 introduced  ProcessHandle and ProcessHandle.Info interface to get all the details about process and it’s information.

ProcessHandle identifies and provides control of native processes. Each individual process can be monitored for liveness, list its children, get information about the process or destroy it.

ProcessHandle.Info gives Information snapshot about the process. The attributes of a process vary by operating system and are not available in all implementations. Information about processes is limited by the operating system privileges of the process making the request.

Let’s discuss some of the important methods with examples:

Process API Features

Get PID of current running process:

getPid() method is used to get the native processId of the process.

 
 //ProcessHandle.current() method will return current process's ProcessHandle
 ProcessHandle currentProcessHandleImpl = ProcessHandle.current();
 
 //getid method return current process Native PID
 System.out.println("Current Process Native PID : "+currentProcessHandleImpl.getPid());

Output:

Current Process Native PID : 3104

Process snapshot of the current running process:

using info() method we can get snapshot of  information about the process

 private static final String NULL = "NULL";
 ProcessHandle currentProcessHandleImpl = ProcessHandle.current();
 ProcessHandle.Info processInfo = currentProcessHandleImpl.info();
 String [] defaults = new String[]{"-"};
 System.out.print("Start arguments : ");
 for(String argument : processInfo.arguments().orElse(defaults)){
 System.out.print(argument+" - ");
 }
 System.out.println("Command : "+processInfo.command().orElse(NULL));
 System.out.println("CommandLine : "+processInfo.commandLine().orElse(NULL));
 System.out.println("User : "+processInfo.user().orElse(NULL));
 System.out.println("Start Time : " + processInfo.startInstant().orElse(Instant.now()).toString());

Output:

Start arguments : - - 
Command : C:\java\jdk-9\bin\java.exe
CommandLine : NULL
User : nitin-PC\nitin
Start Time : 2017-04-01T18:38:58.450Z

Get all childrens process of current running process:

childern() method return direct childrens of the the process:In below example we have created two notepad processes using ProcessBuilder class and called start() method to start that processes.Later on get those child processes and print native PID of that processes.

 ProcessHandle currentProcessHandleImpl = ProcessHandle.current();
 try {
 Process notepadProcessOne = new ProcessBuilder("notepad.exe").start();
 Process notepadProcessTwo = new ProcessBuilder("notepad.exe").start();
 } catch (IOException e) {
 e.printStackTrace();
 }
//collect & iterate all children process n print it's pid 
 List<ProcessHandle> childrenProcess = currentProcessHandleImpl.children().collect(Collectors.toList());
 
 int childrenCount=1;
 for(ProcessHandle processHandle : childrenProcess){
 System.out.println("Children process #"+(childrenCount++) + " PID "+processHandle.getPid());
 }

Output:

Children process #1 PID : 3404
Children process #2 PID : 3644

Get parent process :

parent() method give us parent process:

 
 try {
 Process notepadProcessOne = new ProcessBuilder("notepad.exe").start();
 ProcessHandle parenthandle = notepadProcessOne.toHandle().parent().get();
 System.out.println("Parent Process Native PID : "+parenthandle.getPid());
 } catch (IOException e) {
 e.printStackTrace();
 }

Output:

Parent Process Native PID : 3328

Check Process is Alive or not:

isAlive() method used to check whether process represented by processHandler is alive or not.

 
 try {
 //start notepad process
 Process notepadProcessOne = new ProcessBuilder("notepad.exe").start();
 //get handle of notepad process
 ProcessHandle notepadProcessHandle = notepadProcessOne.toHandle();
 System.out.println("Notepad Process is running : "+notepadProcessHandle.isAlive());
 notepadProcessHandle.destroy(); //destroy notepad process
 System.out.println("Notepad Process is running : "+notepadProcessHandle.isAlive());
 } catch (IOException e) {
 e.printStackTrace();
 }

Output:

Notepad Process is running : true
Notepad Process is running : false

Kill running process:

java 9 provides two methods destroy() and destroyForcibly()  to kill the running process.

Kill running process using destroy() method

 
 try {
 Process notepadProcessOne = new ProcessBuilder("notepad.exe").start();
 ProcessHandle notepadProcessHandle = notepadProcessOne.toHandle();
 System.out.println("Notepad Process is running : "+notepadProcessHandle.isAlive());
 boolean isKilled = notepadProcessHandle.destroy();
 if(isKilled){
 System.out.println("Notepad process killed by destroy() !!!");
 }
 System.out.println("Notepad Process is running : "+notepadProcessHandle.isAlive()+System.lineSeparator());
 } catch (IOException e) {
 e.printStackTrace();
 }
Output:
Notepad Process is running : true
Notepad process killed by destroy() !!!
Notepad Process is running : true

Kill running process using destroyForcibly() method

 try {
 
 Process notepadProcessOne = new ProcessBuilder("notepad.exe").start();
 notepadProcessHandle = notepadProcessOne.toHandle();
 System.out.println("Notepad Process is running : "+notepadProcessHandle.isAlive());
 //killed process 
 isKilled = notepadProcessHandle.destroyForcibly();
 if(isKilled){
 System.out.println("Notepad process killed by destroyForcibly() !!!");
 }
 System.out.println("Notepad Process is running : "+notepadProcessHandle.isAlive()+System.lineSeparator());
 } catch (IOException e) {
 e.printStackTrace();
 }
Output:
Notepad Process is running : true
Notepad process killed by destroyForcibly() !!!
Notepad Process is running : true

Refer ProcessHandle for more details about java process api.

Source Code

Click here to download Source code

You can also download the source code of Process API Example from our git repository.

 

Was this post helpful?

Leave a Reply

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