Java.lang.ProcessBuilder.directory() Method

Advertisements


Description

The java.lang.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

Example

The following example shows the usage of lang.ProcessBuilder.directory() method.

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

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

C:\



Advertisements
Advertisements