Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can getResourceAsStream() find files outside the jar file?

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.

like image 821
kunigami Avatar asked Oct 10 '11 21:10

kunigami


People also ask

Does JAR contain resources?

jar ) contain your executable classes and resource files. A jar can also contain other jar files, which is useful when your program needs some library which is packaged in a jar.

Where is JAR file stored?

A JAR file may contain a manifest file, that is located at META-INF/MANIFEST.


1 Answers

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.

like image 99
Mike Daniels Avatar answered Sep 24 '22 14:09

Mike Daniels