![java package tutorial](/java/lang/images/java.jpg)
- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
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:\