Should ClassLoader::getResouces return the same resource twice?

Alan Bateman Alan.Bateman at oracle.com
Sat Jan 2 07:07:15 UTC 2021


On 02/01/2021 03:21, Thiago Henrique Hupner wrote:
> :
>
> I've created a simple example of what is occurring [1].
> I know there are behavior specific for getting a class if it is in a module,
> but I don't know if this may be a bug in the resource loading mechanism.
> In the example, the returned values are different to illustrate, but in my
> case, it
> returns two exact URLs for the same resource as the source for the module
> reader
> and the classloader is the same.
The behavior you observe with the example is correct. 
ClassLoader.getResources locates the resource by searching the lass 
loader delegation chain and in the example there are two "foo" 
resources. The resource in module fake.module is located because it is 
an automatic module that opens all its packages unconditionally. The 
second resource is located by searching the parent class loader, a 
URLClassLoader in the example that also locates "foo".  Which "foo" did 
you expect to locate? If code in fake.module just wants to locate the 
resource in its own module then it should use getResource rather than 
getResources.

-Alan.


More information about the jigsaw-dev mailing list