How to do it...

  1. Create a new ProcessBuilder object:
        ProcessBuilder pb = new ProcessBuilder();
  1. Set the command to tree and the output and error to the same as that of the current Java process:
  1. Set the directory to whatever directory you want. I set it as the root folder: File("/root"));
  1. Start the process and wait for it to exit:
        Process p = pb.start();
        int exitValue = p.waitFor();
  1. Compile and run using the following commands:
      $ javac -d mods --module-source-path src $(find src -name *.java)
      $ java -p mods -m process/com.packt.process.ChangeWorkDirectoryDemo
  1. The output will be the recursive contents of the directory, specified in the directory() method of the ProcessBuilder object, ...

Get Java 9 Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.