2

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?

A Paul
  • 8,113
  • 3
  • 31
  • 61

5 Answers5

4

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.

4

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");
DavidC
  • 1,842
  • 1
  • 18
  • 32
0

The following code should provide the directory containing the JAR file:

URL url = getClass().getProtectionDomain().getCodeSource().getLocation();
File directory = new File(url.toURI()).getParentFile();
Martin Ellis
  • 9,603
  • 42
  • 53
0

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.

Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66
-1
File f = new File("Folder")

This File object points to "Folder" directory in the the working directory of the Jar.

dARKpRINCE
  • 1,538
  • 2
  • 13
  • 22