- Create a new ProcessBuilder object:
ProcessBuilder pb = new ProcessBuilder();
- Set the command to tree and the output and error to the same as that of the current Java process:
- Set the directory to whatever directory you want. I set it as the root folder:
- Start the process and wait for it to exit:
Process p = pb.start();
int exitValue = p.waitFor();
- 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
- 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.