If I have a collection of resource files in a directory on my classpath, I can enumerate them using ClassLoader.getResources(location)
.
For example if I have /mydir/myresource.properties
on the classpath, I can call the classloader's getResources("mydir")
and get an enumeration of URLs containing myresource.properties
.
When I pack up the exact same resources into a .jar, I don't get anything in the enumeration of URLs when I make the call. I've only replaced the folder structure with a jar containing those folders (it's a webapp, so the jar is going into /WEB-INF/lib
). I've also got a number of other calls using getResourceAsStream(location)
to get other resources individually by name and they're all working fine.
What's different about enumerating resources when the resources are in a .jar?
Update - I've reproduced (ish) the behaviour outside the container. The following snippet results in the dirProperties
object having keys set to the resource names in the package, but if the package is in a .jar throws a NullPointerException during the Properties.load(InputStream) method.
Properties dirProperties = new Properties();
dirProperties.load(this.getClass().getClassLoader().getResourceAsStream(location));
The same code on the container (Tomcat 5.5) throws no exception but produces an empty Properties object when the files are in a .jar.
Methods: JAR file can be added in a classpath in two different ways Step 5: Select the jar file from the folder where you have saved your jar file Step 6: Click on Apply and Ok.
To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course. So basically two methods named: getResource () and getResourceAsStream () are used to load the resources from the classpath. These methods generally return the URL’s and input streams respectively.
Java - classpath /classes: /lib/* In Java 6 wildcard which includes all JAR, it will not search for JARs in a subdirectory. Wildcard is included in all JAR is not honored in the case when we are running Java program with JAR file and having Class-Path attribute in the manifest file.
It can be either Classpath or classpath which is similar to PATH environment variable which we can use to locate Java binaries like javaw and java command. Command 2: By including name of JAR file in -a classpath command-line option
Though I have lot of questions to ask to you to precisely suggest a solution, I can ask you to try this simple step which can help you to find the resources. I believe the class loader which you are using to find the resources is unable to find the resources. So you can try as below (more of a work around):
Package the class along with the other resources in the JAR. Let this class be packed at the same level of folder hierarchy as other resources. Inside JAR, for example :
/resources/ResourceGetter.class
/resources/myprop.properties ....
Use ResourceGetter.class.getClassLoader().getResourceAsStream() or getResources() in your code where you want these resource files.
If you can access the resources, you can find the class loader which loaded it and you can there on solve your problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With