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 a list of ProcessBuilder objects, which will hold the ProcessBuilder instances participating in our pipeline. Also, redirect the output of the last process in the pipeline to the standard output of the current Java process:
         List<ProcessBuilder> pipeline = List.of(
           new ProcessBuilder("cat", "iris.data.txt"),
           new ProcessBuilder("cut", "-d", ",", "-f", "5"),
           new ProcessBuilder("uniq", "-c")
               .redirectOutput(ProcessBuilder.Redirect.INHERIT)
         );
  1. Use the startPipeline() method of ProcessBuilder and pass the list of ProcessBuilder objects to start the pipeline. It will return a list of Process objects, each representing a ProcessBuilder object in the list:
 List<Process> processes = ProcessBuilder.startPipeline(pipeline); ...

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