9

I'm developing an application that uses a library that loads a configuration file as:

InputStream in = getClass().getResourceAsStream(resource);

My application in then packed in a .jar file. If resource is inside the .jar file, I can specify the path as "/relative/path/to/resource/resource".

I'd like to know if is it possible to find the resource if it is outside the .jar file, and in this case, how would I specify the path.

(Assuming my application's jar is in app/ and the resource is in app/config).


The program uses a 3rd party library. The library uses the resource as a configuration file.

I also want to tweak the configuration file without having to unzip/zip the jar file all the time.

kunigami
  • 3,046
  • 4
  • 26
  • 42
  • @AndrewThompson The library uses the resource as a configuration file (it's a thrid party library). I also want to tweak the configuration file without having to unzip/zip the jar file all the time. Actually I forgot to ask if I could accomplish this another way :) – kunigami Oct 11 '11 at 00:21
  • That information is relevant, so I've edited it into the question. Can the 3rd party API be instructed (e.g. by your app.) as to where to look for the resource? Basically what I'm leading to is suggesting not putting it with the app. at all, but in a stable and reproducible path - such as a sub-directory of `user.home`. – Andrew Thompson Oct 11 '11 at 00:35
  • I only just noticed the bit that says 'applet'. Is this actually an applet? That further complicates things. – Andrew Thompson Oct 11 '11 at 00:37
  • @AndrewThompson No, it's not an applet. It'a a regular java application. – kunigami Oct 11 '11 at 09:33

3 Answers3

9

In general, yes it can. Technically, the class's ClassLoader is used to locate the resource named in the parameter (see Javadoc here). If you're not using a special ClassLoader then you'll get the bootstrap class loader, which searches the class path. So, if you have directories or other jar files on the classpath, they will be searched.

Mike Daniels
  • 8,582
  • 2
  • 31
  • 44
1

It will get any resource which is available to the appropriate classloader (the one that getClass().getClassLoader() would return). Whether the classloader includes both the app directory and the jar files depends on the rest of your application context.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

There is a way to get the current directory ( How to get the path of a running JAR file?), but for configuration you can just pass a -Dconfig.location to the JVM specifying an absolute path for the configuration

Community
  • 1
  • 1
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140