Java 9 Process API

In this tutorial, we’re gonna look at new way of retrieving process information: all processes, current process, children processes and destroying process with Java 9 Process API.

I. New Process API
0. ProcessHandle Information

New Process API supports easy way to get many information of a process.
Here is some sample code to retrieve them from a ProcessHandle object:

java.lang.ProcessHandle.Info is a public interface defined within interface java.lang.ProcessHandle.

All parts below will use this printInfo() function to display process information.

1. All processes

We retrieve all ProcessHandle data as a Stream using allProcesses() method.

The result should be similar to:

2. Current process

The result should be similar to:

3. Children processes

To get all children of a process, we use java.lang.ProcessHandle.children() method that return a Stream. Typically, a process which is not alive has no children.

The sample code gets the first process and retrieves its children processes information:

The result should be similar to:

4. Destroy process

To terminate/destroy a running process, we use destroy() method in the ProcessHandle interface.

The result should be similar to:

II. Source code


Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*