ServiceLoader.load(Class, ClassLoader) does not load services exposed in modules and loaded by parent CL

Peter Levart peter.levart at gmail.com
Mon Jun 4 09:09:36 UTC 2018



On 05/29/2018 10:20 PM, Alan Bateman wrote:
> On 29/05/2018 21:00, Jochen Theodorou wrote:
>>
>>
>> Am 24.05.2018 um 19:33 schrieb Robert Scholte:
>> [...]
>>> I can quote Alan:
>>> Rhino used to be the JS engine in older releases and that may have 
>>> been in rt.jar and so loaded by the boot loader. When Nashorn 
>>> replaced it (in JDK 8) then it was configured to be defined to the 
>>> extension class loader so this is why the code snippet doesn't find it.
>>
>> how is this handles in JDK 9 now?
> In the JDK 8, the Nashorn classes were loaded/defined by the extension 
> class loader. In JDK 9, the jdk.scripting.nashorn module is mapped to 
> the platform (formerly extension) class loader. So no real change and 
> the issue that Robert refers to must be an issue with JDK 8 too. I 
> can't quite tell if this is the same as what Peter is seeing but if 
> Maven is creating class loaders with null as the parent then some of 
> the platform classes, and the classes in the service provider modules, 
> will not be visible.
>
> -Alan

The reason why this works on JDK 8 and not on JDK 9+ is that in JDK 8, 
the platform/system services are declared in the META-INF/services as 
resources. While Maven class loader returns null from 
ClassLoader.getParent() it still delegates to extension (platform) class 
loader when searching for resources (ClassLoader.getResources()) and 
loading classes (ClassLoader.loadClass()), so on JDK 8, the service is 
found. Not so on JDK 9, which does not present system/platform services 
to ServiceLoader which is initialized with a ClassLoader of which the 
chain of parent(s) (traversing the ClassLoader.getParent()) does not 
lead to platform class loader. On JDK 9, the platform/system services 
are not declared as META-INF/services resources, but in the 
module-info.class files...

Regards, Peter



More information about the jigsaw-dev mailing list