ClassLoader.getResources(String)

Stephen Colebourne scolebourne at joda.org
Wed Feb 7 16:56:55 UTC 2018


On 7 February 2018 at 16:35, Alan Bateman <Alan.Bateman at oracle.com> wrote:
> On 07/02/2018 14:23, Stephen Colebourne wrote:
>>
>> I've been trying to use ClassLoader.getResources(String). The entire
>> application is in one named module, this includes the code that
>> invokes the ClassLoader method and the resource that it is trying to
>> find.
>
> Can you summarize what you are trying to do? If this is code in a module
> trying to locate one of its own resources then Class.getResourceXXX or
> Module.getResourcAsStream are the candidate APIs to use (not
> ClassLoader.getResourceXXX as that can never locate resources that are
> encapsulated).

I was using maven to create a jar-with-dependencies file, so I could
use jlink. With all the code in one jar file, there shouldn't be any
access barriers to worry about.

ClassLoader.getResources(String) worked just fine until Java 9. The
two APIs are not comparable - the ClassLoader one returns all URLs
found, whereas the Class one returns just one URL. Switching API would
change behaviour.

The code can be seen here:
https://github.com/OpenGamma/Strata/blob/master/modules/collect/src/main/java/com/opengamma/strata/collect/io/ResourceConfig.java#L242
It is a core part of the system that loads configuration at startup.

thanks
Stephen


More information about the jigsaw-dev mailing list