Java
current working directory
change directory
programming
file handling

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:

java
String currentDir = System.getProperty("user.dir");
System.out.println("Current working directory: " + currentDir);

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:

java
1import java.io.IOException;
2
3public class ChangeDirExample {
4    public static void main(String[] args) {
5        ProcessBuilder processBuilder = new ProcessBuilder();
6        processBuilder.directory(new java.io.File("/desired/path"));
7
8        try {
9            Process process = processBuilder.start();
10            // The subprocess will have /desired/path as its working directory
11        } catch (IOException e) {
12            e.printStackTrace();
13        }
14    }
15}

Why setting user.dir does not work

You can technically overwrite the property:

java
System.setProperty("user.dir", "/new/working/directory");

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:

java
1import java.nio.file.Paths;
2import java.nio.file.Path;
3
4public class PathExample {
5    public static void main(String[] args) {
6        Path currentRelativePath = Paths.get("");
7        String currentDir = currentRelativePath.toAbsolutePath().toString();
8        System.out.println("Current relative directory is: " + currentDir);
9
10        Path newRelativePath = Paths.get("/desired/path");
11        String newDir = newRelativePath.toAbsolutePath().toString();
12        System.out.println("New directory path would be: " + newDir);
13    }
14}

This only computes paths; nothing changes globally.

Comparing the options

TechniqueDescriptionProsCons
ProcessBuilderLaunch subprocesses with a different working directoryIsolates directory changes to subprocessesExtra complexity, doesn't change main process
System.setPropertyTheoretically change user.dir propertyEasy to implementMay not be effective, can lead to unstable behavior
Path ManagementProgrammatically manipulating paths rather than changing directoryControl over directory interactionsRequires 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.File and java.nio.file help standardize them.
  • Validate paths. Changing directories, especially for subprocesses, has security implications. Validate paths so a process cannot be pointed at unexpected directories.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data 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.

Practice algorithms