Java IO Tutorial

How to get the current working directory in Java

In Java, we can use System.getProperty("user.dir") to get the current working directory, the directory from where your program was launched.


  String dir = System.getProperty("user.dir");

  // directory from where the program was launched
  // e.g /home/mkyong/projects/core-java/java-io
  System.out.println(dir);

One of the good things about this system property user.dir is we can easily override the system property via a -D argument, for example:


java -Duser.dir=/home/mkyong/ -jar abc.jar

1. Working Directory

The below program shows different ways like File, Paths, FileSystems, or system property to get the current working directory; all methods will return the same result.

CurrentWorkingDirectory.java

package com.mkyong.io.howto;

import java.io.File;
import java.nio.file.FileSystems;
import java.nio.file.Paths;

public class CurrentWorkingDirectory {

    public static void main(String[] args) {

        printCurrentWorkingDirectory1();
        printCurrentWorkingDirectory2();
        printCurrentWorkingDirectory3();
        printCurrentWorkingDirectory4();

    }

    // System Property
    private static void printCurrentWorkingDirectory1() {
        String userDirectory = System.getProperty("user.dir");
        System.out.println(userDirectory);
    }

    // Path, Java 7
    private static void printCurrentWorkingDirectory2() {
        String userDirectory = Paths.get("")
                .toAbsolutePath()
                .toString();
        System.out.println(userDirectory);
    }

    // File("")
    private static void printCurrentWorkingDirectory3() {
        String userDirectory = new File("").getAbsolutePath();
        System.out.println(userDirectory);
    }

    // FileSystems
    private static void printCurrentWorkingDirectory4() {
        String userDirectory = FileSystems.getDefault()
                .getPath("")
                .toAbsolutePath()
                .toString();
        System.out.println(userDirectory);
    }

}

Output

Terminal

/home/mkyong/projects/core-java/java-io
/home/mkyong/projects/core-java/java-io
/home/mkyong/projects/core-java/java-io
/home/mkyong/projects/core-java/java-io

Normally, we use the System.getProperty("user.dir") to get the current working directory.

2. Working Directory for JAR file?

Do not use System.getProperty("user.dir"), File or Path to access a file that is inside a JAR file, it is not going to work. Instead, we should use getClassLoader().getResourceAsStream().


// get a file from the resources folder, root of classpath in JAR
private InputStream getFileFromResourceAsStream(String fileName) {

    // The class loader that loaded the class
    ClassLoader classLoader = getClass().getClassLoader();
    InputStream inputStream = classLoader.getResourceAsStream(fileName);

    // the stream holding the file content
    if (inputStream == null) {
        throw new IllegalArgumentException("file not found! " + fileName);
    } else {
        return inputStream;
    }

}

Note
The above code is extracted from this Read a file from resources folder. Refer to example 2 for accessing a file that is inside a JAR file.

Download Source Code

$ git clone https://github.com/mkyong/core-java

$ cd java-io

References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
13 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Bogdan
6 years ago

When working with Spring MVC on tomcat, for some reason i get a path to tomcat’s “bin” folder from this property

crossRT
10 years ago

Hi mkyong, I found some problem on Linux system, could you help me?
System: Ubuntu 13.10
IDE : Netbeans 7.4
Java version: 1.7.0_45

My application have a configuration file which plan to place on same directory of .jar. But when i generate my project to .jar and run it. Default working directory set to my home folder “/home/[username]/” instead of the location of .jar. So i can’t get the configuration file.

I try “MYCLASS.class.getProtectionDomain().getCodeSource().getLocation().getPath()”, but this is refer to “[PROJECTDIRECTORY]/build/classes” when i debugging on IDE.

is there any good idea to place a file something like configuration file?

Madi
9 years ago

Thanks!!!!!

microssoft
10 years ago

Thanks, not the first time you helped me!!!

minh ho
3 years ago

How to get folder path by getResourceAsStream(), your example may be read the file, not the folder, I need to get the current folder path when running with jar file. Thanks

JinSSJ2
4 years ago

Thankyou very much!
that was the shortest answer I’ve found
to get the path in wich my project is or in other words :
“my current working directory”

Mohammad
5 years ago

but for me it returns the tomcat bin folder address not my application folder

subbu
10 years ago

Other approaches :

package com.subbu;

import java.io.File;

public class CurrentWDR {

public static void main(String[] args) throws Exception {

System.out.println(System.getProperty(“user.dir”));

System.out.println(new File(“.”).getAbsolutePath());

System.out.println(new File(“.”).getCanonicalPath());

System.out.println(new File(“.”).getName());

System.out.println(new File(“.”).getParent());

System.out.println(new File(“.”).getPath());

System.out.println(new File(“.”).getAbsoluteFile());

System.out.println(new File(“.”).getCanonicalFile());

System.out.println(new File(“.”).getParentFile());

}

}

Ece
10 years ago

I’d like to get pictures from users’s current folder in android. User pick a photo from anywhere it wants, then I’ll show the other photos from that directory in next/prev button in android. But I can’t get current folder via user.dir By the way I’m working on android emulator.

Daniel
10 years ago

Hi mkyong,

It’s giving me an error:

Exception in thread “main” java.lang.NoClassDefFoundError:
Caused by: java.lang.ClassNotFoundException:
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

Dan