Java 9: Process API and Version String Formats

DZone 's Guide to

Java 9: Process API and Version String Formats

Here we take a look at a couple of less popular, but still important changes coming with Java 9 — version string formatting and the Process API.

Free Resource

This article explains the Process API improvements in Java 9 and also the changes in the Java strings format.

Process API

Java 9 helps us to control and manage the processes on our operating system by introducing new classes and methods. The Process API lacks some key functionality, so developers have to write a mess of code to perform those tasks. With Java 9, the ProcessHandle Interface helps us get the process information in detail by providing utility methods (process id, information, etc.). It enhances the Process class and introduces ProcessHandle with its nested interface Info to overcome the limitations we had in past.

With this improvement, we can get different properties of each process. For example, we can now obtain the process id, the process name, the user who started the process, the path of the process, resources usage, start time of the process, etc. We can also get the process trees — and manage those trees.

The Process API in Java 9 provides more control over  running and spawned processes. Let's look at the following screenshots for some examples.

Example 1: Some ProcessHandle methods are descibed below:

Image title

Example 2: A few ProcessHandle.info methods are described below:

Image title

Process Information

To obtain  information on all processes, we will be using the allProcesses() method of the ProcessHandle Interface.

public class ProcessDemo {
    public static void main(String[] args) {
        ProcessHandle.allProcesses().filter(p - > p.info().command().isPresent()).limit(4).forEach((process) - > {
            System.out.println("Process id : " + process.pid());
            ProcessHandle.Info info = process.info();
            System.out.println("Command: " + info.command().orElse(""));
            String[] arguments = info.arguments().orElse(new String[] {});
            for (String arg: arguments)
                System.out.println(" arguement :" + arg);
            System.out.println("Command line: " + info.commandLine().orElse(""));
            System.out.println("Start time: " + info.startInstant().orElse(Instant.now()).toString());
            System.out.println("Run time duration: " + info.totalCpuDuration().orElse(Duration.ofMillis(0)).toMillis());
            System.out.println("User :" + info.user().orElse(""));

Triggering Actions on Process Termination

We can trigger actions upon process termination by using the onExit() method of the ProcessHandle interface. In the following example, the process starts Notepad and, on termination, prints the process id.

Image title

We can also destroy all the running processes using the .destroy() method.

Version String Format in Java 9

This might be a smaller, but still very important planned change. Java 9 updated the version string schema. The new changes makes it easier to distinguish major, minor, and critical patch updates. The new convention is [MAJOR].[MINOR].[SECURITY]. The GA version will be 9. So, for instance, the critical patch updates will change the version from 9 to 9.0.1. The minor releases on top of 9.0.1 will change the version string to 9.1.1. Oracle JEP 223 is for the new version string schema.

Due to the change in the version string schema, the code that parses the JDK version string needed to be adjusted. The Runtime.Version class in Java 9 helps us get version information.

  • $MAJOR — Describes the major verison of Java.

  • $MINOR — Describes the bug fixes.

  • $SECURITY — Critical fixes to improve the security for that major release. It will not reset to 0 on minor increases. (Minor and security versions are not related)

Image title

The above snapshot will descibe how to use the Runtime.version() class, which was introduced in the JDK 9 to get the version information. Version is the static inner class of Runtime.

The above example is based on the early access build, so this is not overly informative. In the future, version will output strings like 9.0.1, 9.1.2, etc., which will follow the pattern of  $MAJOR.$MINOR.$SECURITY

Note: The snaphots are executed in jshell and the code block is executed manually by using the javac and java commands.

java ,java 9 ,version ,process api ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}