Home »
Java programming language
Java ProcessBuilder directory() method with example
ProcessBuilder Class directory() method: Here, we are going to learn about the directory() method of ProcessBuilder Class with its syntax and example.
Submitted by Preeti Jain, on December 11, 2019
Syntax:
public File directory ();
public ProcessBuilder directory (File dir);
ProcessBuilder Class directory() method
- directory() method is available in java.lang package.
- directory() method is used to return the working directory of this process builder. If it returns null to indicate the current working directory of the current process so the name of the directory will be assigned by using system property "user.dir" assign.
- directory(File dir) method is used to return the working directory of this process builder. If it sets argument null to indicate the current working directory of the current process so the name of the directory will be assigned by using system property "user.dir".
- These methods don't throw an exception at the time of returning working directory of this process builder.
- These are non-static methods, it is accessible with the class object only and, if we try to access these methods with the class name then we will get an error.
Parameter(s):
- In the first case, this method accepts none parameters.
- In the second case, File dir - This parameter represents the new working directory.
Return value:
In the first case, the return type of the method is File directory() – This parameter represents the working directory of this process builder.
In the second case, the return type of the method is ProcessBuilder, it returns this process builder.
Example:
// Java program to demonstrate the example
// of directory () method of ProcessBuilder class
import java.io.*;
import java.util.*;
public class Directory {
public static void main(String[] args) throws Exception {
// Creating an object of File and List
File fi = new File("E://Programs");
List l = new LinkedList();
// By using add() method to add elements
l.add("TextPad.exe");
l.add("notepad.exe");
// Instantiating ProcessBuilder object
ProcessBuilder pr_bu = new ProcessBuilder(l);
// By using directory() method is to return the working directory
System.out.println("pr_bu.directory() = " + pr_bu.directory());
// By using directory(File dir) method is to set the path of
// the working directory
pr_bu.directory(fi);
System.out.println("pr_bu.directory(fi) = " + pr_bu.directory());
}
}
Output
pr_bu.directory() = null
pr_bu.directory(fi) = E:/Programs