Step By Step Process API in Java

Posted By : Neha Yadav | 09-Mar-2021

Introduction

 

 1. Java 9 added a new feature to control and manage the processes on our OS by introducing new Process API

 2. Java 9 provides a ProcessHandle interface that has some utility methods which will give us information about the process id, process information etc.

 3. It resides in java.lang package.

 4. With this, we will get different properties of each process like name, the user who started it, start time, resource usage.

 5. ProcessHandle interface has a nested interface called Info. Info will provide us the additional information about each and every process availabel by the OS. 

 

Prototype of interface

public interface ProcessHandle extends Comparable<ProcessHandle>

 

Some methods of ProcessHandle Interface:-
getPid(), hashcode(), destroy(), onExit(), current(), allProcessess(), parent(), getClass(), children(), count() etc.

 

Some methods of Info Interface:-

arguments(), isPresent(), command(), user(), commandLine(), startInstant(), totalCpuDuration() etc.

 

Here are some examples(On Jshell):-

 

1. Get current process 

jshell> ProcessHandle process = ProcessHandle.current();
process ==> 67166691]

 

2. Process Info

shell> process.info()
$2 ==> [user: Optional[neha], cmd: /usr/lib/jvm/java-10-oracle/bin/java, args: [-agentlib:jdwp=transport=dt_socket,address=localhost:52298, jdk.jshell.execution.RemoteExecutionControl, 40012], startTime: Optional[2018-07-30T05:19:03.060Z], totalTime: Optional[PT1.08S]]

 

3. Get current process id

jshell> process.getPid()
$4 ==> 6716

 

4. Get current process hashcode

jshell> process.hashCode()
$5 ==> 6716

 

5. Get process parent

jshell> process.parent()
$6 ==> Optional[6691]

 

6. Check process is alive or not

jshell> process.isAlive()
$7 ==> true[6691]

 

7. Count children of a process

-> process.children().count();
|  Expression value is: 0
|    assigned to temporary variable $10 of type long

 

8. Check if process support normal termination

-> process.supportsNormalTermination()
|  Expression value is: true
|    assigned to temporary variable $11 of type boolean

 

By using Info's object and explore more about a process.

-> ProcessHandle.Info processInfo = process.info();

|  Modified variable processInfo of type ProcessHandle.Info with initial value [user: Optional[neha], cmd: /usr/lib/jvm/java-9-openjdk-amd64/bin/java, args: [-classpath, -Xdebug, -Xrunjdwp:transport=dt_socket,address=neha:38281,suspend=y, jdk.internal.jshell.remote.RemoteAgent, 43627], startTime: Optional[2019-01-16T07:15:51.580Z], totalTime: Optional[PT2.36S]]
|    Update overwrote variable processInfo

 

 Some examples of Info :-

 

1. Check if the process has arguments.

-> processInfo.arguments().isPresent();
|  Expression value is: true
|    assigned to temporary variable $14 of type boolean

2.Give executable path name of process.

-> processInfo.command().isPresent();
|  Expression value is: true
|    assigned to temporary variable $15 of type boolean

3. Return the user of the process

-> processInfo.user();
|  Expression value is: Optional[neha]
|    assigned to temporary variable $23 of type Optional<String>

4. Check if any user present

-> processInfo.user().isPresent();
|  Expression value is: true
|    assigned to temporary variable $16 of type boolean

5. Returns the array of strings of the arguments of the process.

-> processInfo.arguments();
|  Expression value is: Optional[[Ljava.lang.String;@305fd85d]
|    assigned to temporary variable $24 of type Optional<String[]>

6. Returns the commandLine of the process.

-> processInfo.commandLine();
|  Expression value is: Optional[/usr/lib/jvm/java-9-openjdk-amd64/bin/java -classpath -Xdebug -Xrunjdwp:transport=dt_socket,address=neha:38281,suspend=y jdk.internal.jshell.remote.RemoteAgent 43627]
|    assigned to temporary variable $18 of type Optional<String>

7. Check if commandLine option is present or not.

-> processInfo.commandLine().isPresent();
|  Expression value is: true
|    assigned to temporary variable $19 of type boolean

8. Tell the total cpu time taken till now

-> processInfo.totalCpuDuration();
|  Expression value is: Optional[PT2.36S]
|    assigned to temporary variable $20 of type Optional<java.time.Duration>

9. Check if total cpu duration present or not.

-> processInfo.totalCpuDuration().isPresent();
|  Expression value is: true
|    assigned to temporary variable $21 of type boolean

 

Thanks

 

About Author

Author Image
Neha Yadav

Neha is a creative person. She is having good knowledge of core java, advance java, hibernate,spring boot. She likes to solve puzzles, sudoku. She is a fun loving person.

Request for Proposal

Name is required

Comment is required

Sending message..