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. Create an instance of ProcessBuilder by providing the required command and its options: 
        ProcessBuilder pBuilder = new ProcessBuilder("free", "-m");

An alternate way to specify the command and options is as follows:

        pBuilder.command("free", "-m");
  1. Set up the input and output streams for the process builder and other properties, such as the directory of execution and environment variables. After that, invoke start() on the ProcessBuilder instance to spawn the process and get a reference to the Process object:
        Process p = pBuilder.inheritIO().start();

The inheritIO() function sets the standard I/O of the spawned subprocess to be the same as that of the current Java process.

  1. We then wait for the completion of the process, ...

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