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

Changing the working directory of a subprocess

Often, you would want a process to be executed in the context of some path, such as listing the files in some directory. In order to do so, we will have to tell ProcessBuilder to launch the process in the context of a given location. We can achieve this by using the directory() method. This method serves two purposes: 

  1. Returns the current directory of execution when we don't pass any parameter.
  2. Sets the current directory of execution to the passed value when we pass a parameter.

In this recipe, we will see how to execute the tree command to recursively traverse all the directories from the current directory and print it in the form of a tree. 

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