BIG
DATA

JAVA

Process API updates in Java 9

Read more about »
  • Java 9 features
  • Read about Hadoop
  • Read about Storm
  • Read about Storm
 

JEP 102: Process API Updates

The main intention of this JEP(JDK Enhancement Proposal) is to improve the API for controlling and managing operating-system processes. For controlling and managing processes on your operating system, Java provides the ProcessAPI.

But in Java 8 and earlier verions the API lacks some key functionality, which makes handling with processes in Java a mess. The limitations of the API often force developers to resort to native code in earlier Java versions. With Java 9, this API will get a considerable update.


What are the new features of the ProcessAPI in Java 9?

The Process API lacks some key functionality, so developers have to write a messy code to perform those tasks. For example, in order to do something as simple as get your process PID in earlier versions of Java, you would need to either access native code or use some sort of a workaround. The process of doing so is very cumbersome.

Java 9 comes to fix those issues and provide a clean API for interaction with processes. It enhances the Process class and introduces ProcessHandle with its nested interface Info to overcome the limitations we had in past. More specifically two new interfaces has been added to the JDK:

  • java.lang.ProcessHandle
  • java.lang.ProcessHandle.Info

See the below code:

public void test_Java_9_process_api() 
  throws IOException {
  
    ProcessHandle processHandle = ProcessHandle.current();
    ProcessHandle.Info processInfo = processHandle.info();
  
    assertNotNull(processHandle.getPid());
    assertEquals(false, processInfo.arguments().isPresent());
    assertEquals(true, processInfo.command().isPresent());
    assertTrue(processInfo.command().get().contains("java"));
    assertEquals(true, processInfo.startInstant().isPresent());
    assertEquals(true, processInfo.user().isPresent());
}

java.lang.ProcessHandle has methods for retrieving a PID, all the processes running in the system and also methods for relationships between processes. The second one java.lang.ProcessHandle.Info provides meta information about the process.

Enumerating Live Processes in the System

In Java 9, we can list all the processes currently in the system, which are visible to the current process. The returned list is a snapshot at the time when the API was invoked, so it’s possible that some processes terminated after taking the snapshot or some new processes were added. In order to do that, we can use the static method allProcesses() available in the java.lang.ProcessHandle interface which returns us a Stream of ProcessHandle.


Conclusion

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.

Using the java.lang.ProcessHandle.Info API, we can now obtain a lot of information about the process like:

  • the command used to start the process
  • the arguments of the command
  • time instant when the process was started
  • total time spent by it and the user who created it