How to get the current working directory in Java?

In Java, the current working directory can be found using the following code:

Code :

public class JavaApplication {
  public static void main(String[] args) {
    System.out.println("Working Directory = " + System.getProperty("user.dir"));

This will print the absolute path of the current directory from where your application was initialized.


From the documentation: package resolve relative pathnames using the current user directory. The current directory is represented as a system property, that is, user.dir and is the directory from where the JVM was invoked.

Method #2:

Using java.nio.file.Path and java.nio.file.Paths, you can do the following to show what Java thinks is your current path. This is for 7 and on, and uses NIO.

Path currentRelativePath = Paths.get("");
String s = currentRelativePath.toAbsolutePath().toString();
System.out.println("Current absolute path is: " + s);

This outputs:

Current absolute path is: /Users/george/NetBeansProjects/Tutorials

that in my case is where I ran the class from.

Constructing paths in a relative way, by not using a leading separator to indicate you are constructing an absolute path, will use this relative path as the starting point.

Method #3:

The following works on Java 7 and up:

import java.nio.file.Paths;


Getting current working directory in Java:

This will give you the path of your current working directory:

Path path = FileSystems.getDefault().getPath(".");

And this will give you the path to a file called “Foo.txt” in the working directory:

Path path = FileSystems.getDefault().getPath("Foo.txt");

Edit : To obtain an absolute path of current directory:

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

* Update * To get current working directory:

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

Answer #4: Current working directory in Java

Java 11 and newer

This solution is better than others and more portable:

Path cwd = Path.of("").toAbsolutePath();

Or even

String cwd = Path.of("").toAbsolutePath().toString();

On Linux when you run a jar file from terminal, these both will return the same String“/home/CurrentUser”, no matter, where youre jar file is. It depends just on what current directory are you using with your terminal, when you start the jar file.



If your Class with main would be called MainClass, then try:


This will return a String with the absolute path of the jar file.

Hope you learned something from this post.

Follow Programming Articles for more!

About ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ

Linux and Python enthusiast, in love with open source since 2014, Writer at, India.

View all posts by ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ →