Java Get User Current Working Directory

Tags: Java working directory System Java File Java Path

In this Java tutorial we learn how to get the user’s current working directory with different solutions. Via different Java examples code you learn how to use system property, Java IO and Java NIO API to get the working directory in a Java application.

Table of contents

  1. Get user working directory in Java using System property
  2. Get current working directory using File API in Java
  3. Get current working directory using FileSystems API in Java
  4. Get current working directory using Paths API in Java

Get user working directory in Java using System property

In the following Java program we use the System.getProperty(“user.dir”) method to get user working directory.

UserDirectoryExample1.java

public class UserDirectoryExample1 {
    public static void main(String... args) {
        String userDirectory = System.getProperty("user.dir");

        System.out.println("User's current working directory: " + userDirectory);
    }
}
The output as below.
User's current working directory: D:\simplesolution\java-examples

Get Current Working Directory using File API in Java

In Java we can create a new instance of java.io.File with empty String parameter to get the working directory as following Java program.

UserWorkingDirectoryExample2.java

import java.io.File;

public class UserWorkingDirectoryExample2 {
    public static void main(String... args) {

        String userDirectory = new File("").getAbsolutePath();

        System.out.println("User's current working directory: " + userDirectory);
    }
}
The output as below.
User's current working directory: D:\simplesolution\java-examples

Get Current Working Directory using FileSystems API in Java

The Java NIO API also provides java.nio.file.FileSystems class allow instantiate new Path object which can be used to get current working directory.

UserWorkingDirectoryExample3.java

import java.nio.file.FileSystems;
import java.nio.file.Path;

public class UserWorkingDirectoryExample3 {
    public static void main(String... args) {

        Path path = FileSystems.getDefault().getPath("");
        String userDirectory = path.toAbsolutePath().toString();

        System.out.println("User's current working directory: " + userDirectory);
    }
}
The output as below.
User's current working directory: D:\simplesolution\java-examples

Or we can write Java code in one line as below.

String userDirectory = FileSystems.getDefault().getPath("").toAbsolutePath().toString();

Get Current Working Directory using Paths API in Java

There is a similar solution to get working directory from Path object but we can use Paths.get() method get the Path object as below Java program.

UserWorkingDirectoryExample4.java

import java.nio.file.Path;
import java.nio.file.Paths;

public class UserWorkingDirectoryExample4 {
    public static void main(String... args) {

        Path path = Paths.get("");
        String userDirectory = path.toAbsolutePath().toString();

        System.out.println("User's current working directory: " + userDirectory);
    }
}
The output as below.
User's current working directory: D:\simplesolution\java-examples

Or we can write it in one line of Java code as below.

String userDirectory = Paths.get("").toAbsolutePath().toString();

Happy Coding 😊