Class.getResourceAsStream () problem

I have a JAR archive with java classes. One of them uses some resource embedded in the same JAR. To download this resource, I use

MyClass.class.getResourceAsStream(myResourceName);

One thing that bothers me is whether it is guaranteed that the required resource will be loaded from one JAR. The documentation for the getResourceAsStream () method (and the corresponding ClassLoader method) is not entirely clear to me.

What happens if a resource with the same name is somewhere in the JVM classpath before my JAR? Will this resource be loaded instead of the built-in in my JAR? Is there any other way to replace a resource embedded in a JAR?

+3
source share
4 answers

. , , , . " ", , .

(, OSGi), Java- .

+2

, . (), , , .

( ResourceBundle - . 6.3 Java Secure Coding Guidelines), URL, ClassLoader.getResourceAsStream URL.openStream .

+1

Specify the package. Assuming you are using com.yourcompany.file, it MUST be unique. (If someone fails to override your configuration file via the classpath.)

+1
source

If you want to read a file only from a specific JAR, you can open the JarFile and read it directly.

+1
source

All Articles