0% found this document useful (0 votes)
4 views7 pages

Process API Updates123

The document discusses the enhancements introduced in Java 9's Process API, which simplify communication with the operating system by allowing developers to easily manage processes without complex native code. Key features include new methods for obtaining process IDs, creating and destroying processes, and accessing process information through the ProcessHandle interface. The document also provides several use cases demonstrating how to utilize these features effectively.

Uploaded by

sydyousuff
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Process API Updates123

The document discusses the enhancements introduced in Java 9's Process API, which simplify communication with the operating system by allowing developers to easily manage processes without complex native code. Key features include new methods for obtaining process IDs, creating and destroying processes, and accessing process information through the ProcessHandle interface. The document also provides several use cases demonstrating how to utilize these features effectively.

Uploaded by

sydyousuff
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Process API Updates (JEP-102)

Unitl java 8, communicating with processor/os/machine is very difficult. We required to write very
complex native code and we have to use 3rd party jar files.

The way of communication with processor is varied from system to system (i.e. os to os). For
example, in windows one way, but in Mac other way. Being a programmer we have to write code
based on operating system, which makes programming very complex.

To resolve this complexity, JDK 9 engineers introduced several enhancements to Process API. By
using this Updated API, we can write java code to communicate with any processor very easily.
According to worldwide Java Developers, Process API Updates is the number 1 feature in Java 9.

With this Enhanced API, we can perform the following activities very easily.

1. Get the Process ID (PID) of running process.


2. Create a new process
3. Destroy already running process
4. Get the process handles for processes
5. Get the parent and child processes of running process
6. Get the process information like owner, children,...
etc...

What's New in Java 9 Process API:


1. Added several new methods (like pid(),info() etc) to Process class.

2. Added several new methods (like startPipeline()) to ProcessBuilder class. We can use
ProcessBuilder class to create operating system processes.

3. Introduced a new powerful interface ProcessHandle. With this interface, we can access current
running process, we can access parent and child processes of a particular process etc

4. Introduced a new interface [Link], by using this we can get complete information
of a particular process.

Note: All these classes and interfaces are part of [Link] package and hence we are not required
to use any import statement.

~1~
How to get ProcessHandle object:
It is the most powerful and useful interface introduced in java 9.
We can get ProcessHandle object as follows

1. ProcessHandle handle=[Link]();
Returns the ProcessHandle of current running Process

2. ProcessHandle handle=[Link]();
Returns the ProcessHandle of specified Process object.

3. Optional<ProcessHandle> handle=[Link](PID);
Returns the ProcessHandle of proccess with the specified pid.
Here, the return type is Optional, because PID may exist or may not exist.

Use Case-1: To get the process ID (PID) of current process


1) public class Test
2) {
3) public static void main(String[] args) throws Exception
4) {
5) ProcessHandle p=[Link]();
6) long pid=[Link]();
7) [Link]("The PID of current running JVM instance :"+pid);
8) [Link](100000);
9) }
10) }

We can see this process id in Task Manager (alt+ctrl+delete in windows)

[Link]:
We can get complete information of a particular process by using [Link] object.
We can get this Info object as follows.

ProcessHandle p = [Link]();
[Link] info = [Link]();

Once we got Info object, we can call the following methods on that object.

1. user():
Return the user of the process.
public Optional<String> user()

2. command():
Returns the command,that can be used to start the process.
public Optional<String> command()

~2~
3. startInstant():
Returns the start time of the process.
public Optional<String> startInstant()

4. totalCpuDuration():
Returns the total cputime accumulated of the process.
public Optional<String> totalCpuDuration()

Use Case-2: To get snapshot of the current running process info


1) public class Test
2) {
3) public static void main(String[] args) throws Exception
4) {
5) ProcessHandle p=[Link]();
6) [Link] info=[Link]();
7) [Link]("Complete Process Inforamtion:\n"+info);
8) [Link]("User: "+[Link]().get());
9) [Link]("Command: "+[Link]().get());
10) [Link]("Start Time: "+[Link]().get());
11) [Link]("Total CPU Time Acquired: "+[Link]().get());
12) }
13) }

Use Case-3: To get snapshot of the Particular Process Based on id


1) import [Link].*;
2) public class Test
3) {
4) public static void main(String[] args) throws Exception
5) {
6) Optional<ProcessHandle> opt=[Link](7532);
7) ProcessHandle p=[Link]();
8) [Link] info=[Link]();
9) [Link]("Complete Process Inforamtion:\n"+info);
10) [Link]("User: "+[Link]().get());
11) [Link]("Command: "+[Link]().get());
12) [Link]("Start Time: "+[Link]().get());
13) [Link]("Total CPU Time Acquired: "+[Link]().get());
14) }
15) }

~3~
ProcessBuilder:
We can use ProcessBuilder to create processes.
We can create ProcessBuilder object by using the following constructor.

public ProcessBuilder(String... command)

The argument should be valid command to invoke the process.

Eg:
ProcessBuilder pb = new ProcessBuilder("javac","[Link]");
ProcessBuilder pb = new ProcessBuilder("java","Test");
ProcessBuilder pb = new ProcessBuilder("[Link]","D:\\[Link]");

Once we create a ProcessBuilder object,we can start the process by using start() method.

[Link]();

Use Case-4: To create and Start a process by using ProcessBuilder


[Link]:

1) import [Link].*;
2) import [Link].*;
3) public class FrameDemo
4) {
5) public static void main(String[] args)
6) {
7) Frame f = new Frame();
8) [Link]( new WindowAdapter()
9) {
10) public void windowClosing(WindowEvent e)
11) {
12) [Link](0);
13) }
14) });
15) [Link](new Label("This Process Started from Java by using ProcessBuilder !!!"));
16) [Link](500,500);
17) [Link](true);
18) }
19) }

~4~
[Link]

1) public class Test


2) {
3) public static void main(String[] args) throws Exception
4) {
5) ProcessBuilder pb=new ProcessBuilder("java","FrameDemo");
6) [Link]();
7) }
8) }

Use Case-5: To open a file with notepad from java by using ProcessBuilder

1) public class Test


2) {
3) public static void main(String[] args) throws Exception
4) {
5) new ProcessBuilder("[Link]","[Link]").start();
6) }
7) }

Use Case-6: To start and destroy a process from java by using ProcessBuilder

1) class Test
2) {
3) public static void main(String[] args) throws Exception
4) {
5) ProcessBuilder pb=new ProcessBuilder("java","FrameDemo");
6) Process p=[Link]();
7) [Link]("Process Started with id:"+[Link]());
8) [Link](10000);
9) [Link]("Destroying the process with id:"+[Link]());
10) [Link]();
11) }
12) }

Use Case-7: To destroy a process which is not created from Java


1) import [Link].*;
2) class Test
3) {
4) public static void main(String[] args) throws Exception
5) {
6) Optional<ProcessHandle> optph=[Link](5232);
7) ProcessHandle ph=[Link]();
8) [Link]();
9) }
10) }

~5~
Use Case-8: To display all running process information
1) import [Link].*;
2) import [Link].*;
3) import [Link].*;
4) class Test
5) {
6) public static void dumpProcessInfo(ProcessHandle p)
7) {
8) [Link] info=[Link]();
9) [Link]("Process Id:"+[Link]());
10) [Link]("User: "+[Link]().orElse(""));
11) [Link]("Command: "+[Link]().orElse(""));
12) [Link]("Start Time: "+[Link]().orElse([Link]()).toString());
13) [Link]("Total CPU Time Acquired: "+[Link]().
14) orElse([Link](0)).toMillis());
15) [Link]();
16) }
17) public static void main(String[] args) throws Exception
18) {
19) Stream<ProcessHandle> allp=[Link]();
20) [Link](100).forEach(ph->dumpProcessInfo(ph));
21) }
22) }

Use Case-9: To display all child process information


1) import [Link].*;
2) import [Link].*;
3) class Test
4) {
5) public static void dumpProcessInfo(ProcessHandle p)
6) {
7) [Link] info=[Link]();
8) [Link]("Process Id:"+[Link]());
9) [Link]("User: "+[Link]().orElse(""));
10) [Link]("Command: "+[Link]().orElse(""));
11) [Link]("Start Time: "+[Link]().orElse([Link]()).toString());
12) [Link]("Total CPU Time Acquired: "+[Link]()
13) .orElse([Link](0)).toMillis());
14) [Link]();
15) }
16) public static void main(String[] args) throws Exception
17) {
18) ProcessHandle handle=[Link]();
19) Stream<ProcessHandle> childp=[Link]();
20) [Link](ph->dumpProcessInfo(ph));
21) }

~6~
22) }

Note: If Current Process not having any child processes then we won't get any output

Use Case-10: To perform a task at the time of Process Termination


1) import [Link].*;
2) public class Test
3) {
4) public static void main(String[] args) throws Exception
5) {
6) ProcessBuilder pb=new ProcessBuilder("java","FrameDemo");
7) Process p=[Link]();
8) [Link]("Process Started with id:"+[Link]());
9) CompletableFuture<Process> future=[Link]();
10) [Link](p1->[Link]("Process Terminated with Id:"+[Link]()));
11) [Link]([Link]());
12) }
13) }

Output [In Normal Termination]:

D:\durga_classes>java Test
Process Started with id:4828
Process[pid=4828, exitValue=0]
Process Terminated with Id:4828

Output [In Abnormal Termination alt+ctrl+delete]:

D:\durga_classes>java Test
Process Started with id:12512
Process[pid=12512, exitValue=1]
Process Terminated with Id:12512

Observe exit value: 0 for normal termination and non-zero for abnormal termination

~7~

You might also like