Can getResourceAsStream () find files outside jar file?

I am developing an application that uses a library that loads a configuration file like:

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

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

I would like to know if a resource can be found if it is outside the .jar file, and in this case, how would I specify the path.

(Suppose my application bar is in app/ and the resource is in app/config ).


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

I also want to configure the configuration file without having to unzip / archive the jar file.

+7
source share
3 answers

In general, yes, it can. Technically, the ClassLoader class ClassLoader used to search for the resource named in the parameter ( see Javadoc here ). If you are not using a special ClassLoader , then you will get a bootstrap class loader that looks for the class path. So, if you have directories or other jar files in the classpath, they will be found.

+8
source

It will receive any resource available to the corresponding classloader (the one that will be returned by getClass().getClassLoader() ). Regardless of whether the classloader contains both the app directory and jar files, it depends on the rest of your application context.

+1
source

There is a way to get the current directory ( How to get the path to the running JAR file? ), But for configuration, you can simply pass -Dconfig.location to the JVM, which defines the absolute path for the configuration

0
source

All Articles