How to change the current working directory in Java
Codemia Knowledge Hub ·
Java has no supported way to change the working directory of a running JVM. You can read it with System.getProperty("user.dir"), start subprocesses in a different directory with ProcessBuilder.directory(), or resolve file paths against a base directory of your choice instead of relying on the working directory.
Getting the current working directory
The working directory is the directory the JVM was launched from. Read it through the user.dir system property:
Running a subprocess in another directory with ProcessBuilder
ProcessBuilder.directory() sets the working directory of a new process. It does not change the directory of your Java program, but it lets subprocesses run somewhere else:
Why setting user.dir does not work
You can technically overwrite the property:
This is not recommended. It may have no meaningful effect, because most Java operations still use the directory the JVM was started in, and it can make the application behave unpredictably.
Managing paths instead of changing directory
The reliable approach is to leave the working directory alone and build paths explicitly:
This only computes paths; nothing changes globally.
Comparing the options
| Technique | Description | Pros | Cons |
ProcessBuilder | Launch subprocesses with a different working directory | Isolates directory changes to subprocesses | Extra complexity, doesn't change main process |
System.setProperty | Theoretically change user.dir property | Easy to implement | May not be effective, can lead to unstable behavior |
| Path Management | Programmatically manipulating paths rather than changing directory | Control over directory interactions | Requires careful coding with absolute/relative paths |
Avoiding working-directory bugs
- Do not depend on the launch directory. Code that assumes a particular working directory is brittle. Prefer absolute paths or configuration parameters.
- Keep paths portable. Paths differ across operating systems;
java.io.Fileandjava.nio.filehelp standardize them. - Validate paths. Changing directories, especially for subprocesses, has security implications. Validate paths so a process cannot be pointed at unexpected directories.
Related reading
- CharSequence VS String in Java?
- Check for the existence of a Kafka topic programatically in Java
- Check if a Class Object is subclass of another Class Object in Java
- Check if a value exists in ArrayList
- Check if null Boolean is true results in exception
- Check two arguments in Java, either both not null or both null elegantly
- Checking for a null int value from a Java ResultSet
- Checking if a string is empty or null in Java
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.