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.
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
/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
When working with Spring MVC on tomcat, for some reason i get a path to tomcat’s “bin” folder from this property
We can override the system property
user.dir
, pass a-Duser.dir=/home/mkyong/
to the VM arguments.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?
One of the options, we can pass a system property to tell about the file location.
java -Dconfig.file=/home/mkyong/file.txt -jar xxx.jar
In Java, read the system property and process the file.
System.getProperty("config.file");
Thanks!!!!!
Thanks, not the first time you helped me!!!
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
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”
but for me it returns the tomcat bin folder address not my application folder
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());
}
}
nice try.
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.
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