Say I have an exported Jar file on my Desktop, and then a folder I want to access so my jar file can load files from it. How can I access that folder without hard coding the path?
-
Use the same path you used to export? – Aniket Thakur Dec 16 '13 at 10:55
-
Do you want to access a folder where your Jar file resides? – dARKpRINCE Dec 16 '13 at 10:55
-
But the user can change the path. – Dec 16 '13 at 10:55
-
by passing arguments java myJar -DloadFilesFrom=C:\something\ and in your program System.getProperty("loadFilesFrom"); – RaceBase Dec 16 '13 at 10:55
-
Please provide what you have tried? – A Paul Dec 16 '13 at 10:56
-
@dARKpRINCE I want to access a folder in the working directory of the jar. – Dec 16 '13 at 10:56
-
Have you considered storing the files within the .jar itself and using a Classloader (I think :s) to access them? – Levenal Dec 16 '13 at 10:57
-
I can't do that actually, that's why I have a separate folder @Levenal – Dec 16 '13 at 10:58
-
My answer answer describes what you want. – dARKpRINCE Dec 16 '13 at 11:03
-
possible duplicate of [How to get the path of a running JAR file?](http://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file) – Andrey Chaschev Dec 16 '13 at 11:03
5 Answers
If I clearly understand, you may use relative path when you try to access folder. For example, if you run your jar as a standalone application, relative path "." will be a folder that contains your jar. So, when you place names.txt file next to your jar, you can get access to it with:
new File("./names.txt");
Hope I understood you right way and this will help.

- 113
- 6
See here: https://stackoverflow.com/a/6849255/514463
Pick one of your classes that you want the directory of, e.g.: Test:
String path = Test.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String decodedPath = URLDecoder.decode(path, "UTF-8");

- 1,842
- 1
- 18
- 32
The following code should provide the directory containing the JAR file:
URL url = getClass().getProtectionDomain().getCodeSource().getLocation();
File directory = new File(url.toURI()).getParentFile();

- 9,603
- 42
- 53
Hmmm...
I heard this question so often, and it always boils down to this: How to load resources at runtime?
The main reason for this type of question is, that one is developping an application and now wants to create a distributable package. This normally ends in something like that:
... / my-application-folder
| -- lib / *.jar
| -- main.jar
| -- config.properties
There could be several configuration files. A configuration for the application itself, a configuration for a logging framework that is used, etc. It does not matter. If you want to access such resources, you should do it in two steps:
1) Make sure all folders containing your resources (such configuration files are resources) are part of the classpath. If you run your JAR file (here main.jar) with a java -jar main.jar
command, this JAR file should contain a manifest file containing the directory .
and all needed library JARs in the class-path entry. [Alternative: Maybe all your config files are located in a config/ subfolder. Then this folder must be part of the class-path.]
2) Inside your application you access such resources with a class loader:
ClassLoader loader = Thread.currentThread().getContextClassLoader();
URL url = loader.getResource(neededResource);
// do something with url, e.g. open stream, read from it, and close stream
If you have a user customizable path, there are several possibilities. You could for example pass a system property to the application, as Reddy suggested in his comment. You could also have a property in a configuration file, which you are loading in the beginning part of your application. However, you have the path to this customizable folder in hand (at runtime). Then you do the following:
String customizablePath = getCustomizablePath();
URL customizablePathURL = new File(customizablePath).toURI().toURL();
ClassLoader loader = new URLClassLoader(new URL[] {customizablePathURL});
Then you can continue like above.

- 20,273
- 6
- 37
- 66
File f = new File("Folder")
This File object points to "Folder" directory in the the working directory of the Jar.

- 1,538
- 2
- 13
- 22