Java 9 - ProcessHandle and ProcessHandle.Info Example

[Updated: Nov 6, 2017, Created: Nov 4, 2017]

The java.lang.Process class has been enhanced to provide an instance of ProcessHandle via Process.toHandle() method. The new interface ProcessHandle can provide the operating system specific process id and can return various other useful objects related to the process. The nested class ProcessHandle.Info provides information about the process.

ProcessHandle and ProcessHandle.Info interfaces

package java.lang;
 ...
public interface ProcessHandle extends Comparable<ProcessHandle> {
    long pid();
    Optional<ProcessHandle> parent();
    Stream<ProcessHandle> children();
    Info info();
    CompletableFuture<ProcessHandle> onExit();
    boolean supportsNormalTermination();
    boolean destroy();
    boolean destroyForcibly();
    boolean isAlive();

    Stream<ProcessHandle> descendants();
       public static Optional<ProcessHandle> of(long pid) {
        return .....
    }
    public static ProcessHandle current() {
        return ....;
    }
    static Stream<ProcessHandle> allProcesses() {
        return ......;
    }
    ......
    public interface Info {
      public Optional<String> command();
      public Optional<String> commandLine();
      public Optional<String[]> arguments();
      public Optional<Instant> startInstant();
      public Optional<Duration> totalCpuDuration();
      public Optional<String> user();
  }
}

Example

In following example, we will start a process, will get various information about it and then we will destroy it.

public class ProcessHandleExample {
  public static void main(String[] args) throws Exception {
      ProcessBuilder processBuilder = new ProcessBuilder("notepad.exe");
      Process process = processBuilder.start();

      System.out.println("-- process handle --");
      ProcessHandle processHandle = process.toHandle();
      System.out.printf("PID: %s%n", processHandle.pid());
      System.out.printf("isAlive: %s%n", processHandle.isAlive());

      System.out.println("-- process info --");
      ProcessHandle.Info info = processHandle.info();
      info.command().ifPresent(str -> System.out.printf("Command: %s%n", str));
      info.commandLine().ifPresent(str -> System.out.printf("CommandLine: %s%n", str));
      info.arguments().ifPresent(array -> System.out.printf("Arguments: %s%n", array));
      info.startInstant().ifPresent(instant -> System.out.printf("StartInstant: %s%n", instant));
      info.totalCpuDuration().ifPresent(duration ->
              System.out.printf("CpuDuration: %s millis%n", duration.toMillis()));
      info.user().ifPresent(str -> System.out.printf("User: %s%n", str));

      System.out.println("-- destroying --");
      processHandle.destroy();
      //better use onExit to know when process exits (destroy() may not kill the process immediately)
      CompletableFuture<ProcessHandle> future = processHandle.onExit();
      ProcessHandle ph = future.get();//blocks
      System.out.printf("isAlive: %s%n", ph.isAlive());
  }
}

Output

-- process handle --
PID: 2536
isAlive: true
-- process info --
Command: C:\Windows\System32\notepad.exe
StartInstant: 2017-11-04T19:34:44.225Z
CpuDuration: 31 millis
User: JOEMCHN\Joe
-- destroying --
isAlive: false

Example Project

Dependencies and Technologies Used :

  • JDK 9.0.1
Java 9 ProcessHandle Example Select All Download
  • process-builder-example
    • src
      • com
        • logicbig
          • example

See Also