Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I find resources in a .jar on the classpath?
    primarykey
    data
    text
    <p>If I have a collection of resource files in a directory on my classpath, I can enumerate them using <code>ClassLoader.getResources(location)</code>.</p> <p>For example if I have <code>/mydir/myresource.properties</code> on the classpath, I can call the classloader's <code>getResources("mydir")</code> and get an enumeration of URLs containing <code>myresource.properties</code>.</p> <p>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 <code>/WEB-INF/lib</code>). I've also got a number of other calls using <code>getResourceAsStream(location)</code> to get other resources individually by name and they're all working fine.</p> <p>What's different about enumerating resources when the resources are in a .jar?</p> <hr> <p>Update - I've reproduced (ish) the behaviour outside the container. The following snippet results in the <code>dirProperties</code> 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.</p> <pre><code>Properties dirProperties = new Properties(); dirProperties.load(this.getClass().getClassLoader().getResourceAsStream(location)); </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload