How to get the current working directory in Java

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

WorkingDirApp.java

package com.mkyong;

import java.nio.file.Paths;

public class WorkingDirApp {

    public static void main(String[] args) {

        System.out.println(System.getProperty("user.dir"));

        // Java 7
        System.out.println(Paths.get("").toAbsolutePath().toString());

    }

}

Output


D:\projects\java\resources
D:\projects\java\resources

References

About the Author

author image
mkyong
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

avatar
14 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
13 Comment authors
JinSSJ2SaraMohammadBogdanMadi Recent comment authors
newest oldest most voted
crossRT
Guest
crossRT

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?

Bogdan
Guest
Bogdan

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

Kimsros
Guest
Kimsros

Hello, I want to create pdf file and store on a directory of Linux Server for example home directory using java code. Could you please show some code? Thanks.

Madi
Guest
Madi

Thanks!!!!!

Sun Yi
Guest
Sun Yi

nice tip, i’ve subscribed to your rss.
thanks for sharing

trackback
How to construct a file path in Java | J2EE Web Development Tutorials
we
Guest
we

alert(‘Salam’);

microssoft
Guest
microssoft

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

Daniel
Guest
Daniel

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

Ece
Guest
Ece

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.

subbu
Guest
subbu

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());

}

}

Mohammad
Guest
Mohammad

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

Sara
Guest
Sara

Hi Mkyong,

Would you please help me? I’m trying to read a multipart file(excel), it works when I copy it to a specific directory on my drive.
What I am trying to do is to read the actual path of the original file so I can read it from there only without the need to make a copy of the file.

Looking forward to your reply.
Best regards and thanks
Sara

JinSSJ2
Guest
JinSSJ2

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”