javaresourcesclasspathclassloadergetresource

How to use ClassLoader.getResources() correctly?


How can I use ClassLoader.getResources() to find recursivly resources from my classpath?

E.g.

And as a bonus question: How does ClassLoader.getResources() differ from ClassLoader.getResource()?


Solution

  • There is no way to recursively search through the classpath. You need to know the Full pathname of a resource to be able to retrieve it in this way. The resource may be in a directory in the file system or in a jar file so it is not as simple as performing a directory listing of "the classpath". You will need to provide the full path of the resource e.g. '/com/mypath/bla.xml'.

    For your second question, getResource will return the first resource that matches the given resource name. The order that the class path is searched is given in the javadoc for getResource.