Java ProcessBuilder directory() Method



Description

The Java ProcessBuilder directory(File directory) method sets this process builder's working directory. Subprocesses subsequently started by this object's start() method will use this as their working directory. The argument 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 ProcessBuilder directory(File directory)

Parameters

directory − The new working directory

Return Value

This method returns this process builder

Exception

NA

Getting directory details for 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 of passed drive.

package com.tutorialspoint;

import java.io.File;

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);

      // set the working directory of the process
      pb.directory(new File("C:/"));
      System.out.println("" + pb.directory());
   }
}

Output

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

C:\

Getting directory details for 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 of passed drive.

package com.tutorialspoint;

import java.io.File;

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);

      // set the working directory of the process
      pb.directory(new File("C:/"));
      System.out.println("" + pb.directory());
   }
}

Output

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

C:\

Getting directory details for 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 of passed drive.

package com.tutorialspoint;

import java.io.File;

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);

      // set the working directory of the process
      pb.directory(new File("C:/"));
      System.out.println("" + pb.directory());
   }
}

Output

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

C:\
java_lang_processbuilder.htm
Advertisements