Java ProcessBuilder directory() Method



Description

The Java ProcessBuilder directory() method returns this process builder's working directory. Subprocesses subsequently started by this object's start() method will use this as their working directory. The returned value may be null − this means to use the working directory of the current Java process, usually the directory named by the system property user.dir, as the working directory of the child process.

Declaration

Following is the declaration for java.lang.ProcessBuilder.directory() method

public File directory()

Parameters

NA

Return Value

This method returns this process builder's working directory

Exception

NA

Getting directory details of Notepad from a Process Builder Example

The following example shows the usage of ProcessBuilder directory() method. In this program, we've created an array of Strings and added notepad.exe and test.txt to it. Using that list, we've initialized a ProcessBuilder instance. Now using directory() method, we've printed the underlying directory details.

package com.tutorialspoint;

public class ProcessBuilderDemo {

   public static void main(String[] args) {

      // create a new list of arguments for our process
      String[] list = {"notepad.exe", "test.txt"};

      // create the process builder
      ProcessBuilder pb = new ProcessBuilder(list);

      // get the working directory of the process
      System.out.println(pb.directory());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

null

Getting directory details of Calculator from a Process Builder Example

The following example shows the usage of ProcessBuilder directory() method. In this program, we've created an array of Strings and added calc.exe to it. Using that list, we've initialized a ProcessBuilder instance. Now using directory() method, we've printed the underlying directory details.

package com.tutorialspoint;

public class ProcessBuilderDemo {

   public static void main(String[] args) {

      // create a new list of arguments for our process
      String[] list = {"calc.exe"};

      // create the process builder
      ProcessBuilder pb = new ProcessBuilder(list);

      // get the working directory of the process
      System.out.println(pb.directory());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

null

Getting directory details of Windows Explorer from a Process Builder Example

The following example shows the usage of ProcessBuilder directory() method. In this program, we've created an array of Strings and added explorer.exe to it. Using that list, we've initialized a ProcessBuilder instance. Now using directory() method, we've printed the underlying directory details.

package com.tutorialspoint;

public class ProcessBuilderDemo {

   public static void main(String[] args) {

      // create a new list of arguments for our process
      String[] list = {"explorer.exe"};

      // create the process builder
      ProcessBuilder pb = new ProcessBuilder(list);

      // get the working directory of the process
      System.out.println(pb.directory());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

null
java_lang_processbuilder.htm
Advertisements