Process API Updates in Java
Last Updated :
23 Feb, 2022
By Process API, we can perform any operation related to a process.
Suppose if we want the process id of current running process, or we want to create a new process, or want to destroy already running process, or want to find the child and parent processes of current running process, or if we want to get any information regarding a process, then we can use Process API updates.
Java 9 Process API Updates: -
- Some new methods are added to the Process class present in java.lang package.
The process class is an old class in Process API, only some methods like, pid(), info() etc are added in java 9. - Methods like startPipeline() are added to ProcessBuilder class which is already present in old java versions.
- ProcessHandle(I) : This is a new interface added in java 9. It is used to handle a process.
- ProcessHandle.Info(I): It is an inner interface, it provides all the information related to a process.
ProcessHandle(I): -
- To get ProcessHandle object of current running process:
ProcessHandle ph = ProcessHandle.current();
- To get ProcessHandle of the given process object:
ProcessHandle ph = p.toHandle(); //p is a process object
- To get ProcessHandle object from a given process ID:
Optional obj = ProcessHandle.of(PID);
ProcessHandle ph = obj.get();
- //getting process handle object
Here the return type of ProcessHandle object is optional because the process may or may not exist.
If the process exist then we will get its ProcessHandle object otherwise we won't get its ProcessHandle object.
Java
public class Demo {
public static void main(String[] args)
{
ProcessHandle ph = ProcessHandle.current();
long id = ph.pid();
System.out.println("Id of the current process is : " + id);
}
}
Id of the current process is : 5420
ProcessHandle.Info(I): -
Info is an inner interface present inside ProcessHandle interface.
We can get complete information of the given or current running process.
To create ProcessHandle.Info object: -
For this, first we need to create ProcessHandle object and then we will create ProcessHandle.Info object.
ProcessHandle ph = ProcessHandle.current();
ProcessHandle.Indo pinfo = ph.info();
Methods in ProcessHandle.Info(I): -
- user(): -
returns the user of the current process.
Optional o = info.user();
System.out.println("User is : "+o.get());
- command(): -
returns the command by which the process is started.
Optional o = info.command();
System.out.println("Command is : "+o.get());
- startInstant(): -
returns the time at which the current process started.
Optional o = info.startInstant();
System.out.println("Time of process start is : "+o.get());
- totalCpuDuration(): -
returns the total CPU duration of the current process.
Optional o = info.totalCpuDuration();
System.out.println("Total CPU duration is : "+o.get());
Example: -
Java
public class Demo {
public static void main(String[] args)
{
ProcessHandle ph = ProcessHandle.current();
ProcessHandle.Info pinfo = ph.info();
System.out.println("Complete process information is :" + pinfo);
System.out.println("User of the process is : " + pinfo.user().get());
System.out.println("Command used is : " + pinfo.command().get());
System.out.println("Time of process starting is : " + pinfo.startInstant().get());
System.out.println("Total CPU Duration is : " + pinfo.totalCpuDuration().get());
}
}
Similar Reads
Java.lang.ProcessBuilder class in Java This class is used to create operating system processes. Each ProcessBuilder instance manages a collection of process attributes. The start() method creates a new Process instance with those attributes. The start() method can be invoked repeatedly from the same instance to create new subprocesses wi
8 min read
Introduction to Processing | Java Processing is an open-source programming language and development environment that is built on top of the Java programming language. It is specifically designed for artists, designers, and other creative professionals who want to create interactive graphics, animations, and other visual applications
6 min read
Introduction to Processing | Java Processing is an open-source programming language and development environment that is built on top of the Java programming language. It is specifically designed for artists, designers, and other creative professionals who want to create interactive graphics, animations, and other visual applications
6 min read
Java.lang.Process class in Java The abstract Process class is a process that is, an executing program. Methods provided by the Process are used to perform input, and output, waiting for the process to complete, checking the exit status of the process, and destroying the process. It extends class Object.It is used primarily as a su
5 min read
Java Program to Update the List Items In Java, Lists are dynamic collections that allow modifications, such as updating elements. We can use the set method to update the elements in the List. This method replaces the element at the given index with a new value, allowing modification of list contents. In this article, we will learn to up
2 min read
Calling an External Program in Java using Process and Runtime Java contains the functionality of initiating an external process - an executable file or an existing application on the system, such as Google Chrome or the Media Player- by simple Java code. One way is to use following two classes for the purpose: Process class Runtime class The Process class pres
2 min read