O'Reilly logo

Java 9 Cookbook by Nick Samoylov, Mohamed Sanaulla

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Spawn a new process from the Java code, which runs the sleep command:
        ProcessBuilder pBuilder = new ProcessBuilder("sleep", "20");
        Process p = pBuilder.inheritIO().start();
  1. Get the ProcessHandle instance for this spawned process:
        ProcessHandle handle = p.toHandle();
  1. Wait for the spawned process to complete execution:
        int exitValue = p.waitFor();
  1. Use ProcessHandle to get the ProcessHandle.Info instance and use its APIs to get the required information. Alternatively, we can even use the Process object directly to get ProcessHandle.Info by using the info() method in the Process class:
 ProcessHandle.Info info = handle.info(); System.out.println("Command line: " + info.commandLine().get()); System.out.println("Command: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required