Layers and NoClassDefFoundError

Alex Buckley alex.buckley at oracle.com
Tue Oct 13 18:13:14 UTC 2015


On 10/13/2015 5:49 AM, Peter Levart wrote:
> I don't know for sure, but I see a Layer as a combination of 2 aspects
> that have to be soundly aligned:
>
> - readability graph of modules
> - delegation of class-loaders
>
> For example, say you have a layer with 2 modules: moda & modb and two
> ClassLoaders loadera & loaderb.
>
> If loaderb is a child of loadera and moda is loaded by loadera while
> modb is loaded by loaderb, then the following reads edge is possible:
>
> - modb reads moda
>
> but the following is not:
>
> - moda reads modb
>
> So to answer your question, on which loader in a parent layer to base
> the child class-loader in a child layer. The one that can (by delegation
> and by itself) load classes in modules that modules in child layer
> loaded by child class loader depend on.

This is a great sketch of the relationship between loaders and layers.

Note that readability "comes first" -- in order to create a Layer, you 
must have already performed module resolution and obtained a 
Configuration. The reads edges indicated by the Configuration should 
drive the delegation between the loader[s] given to Layer::create. 
(Given indirectly, as the range of the ClassLoaderFinder function.)

Of course, loader delegation is nothing more than the run time behavior 
of loadClass method bodies, so there is no way to predict or analyze 
delegation ahead of time. Thus, Layer::create cannot check that the 
ClassLoaderFinder only finds "good" loaders that delegate in accordance 
with the reads edges. Technically there is nothing to stop you creating 
a Layer for the "not possible" situation above:
- a Configuration where moda reads modb;
- a ClassLoaderFinder where moda is found in loadera and modb is found 
in loaderb;
- where loaderb-delegates-to-loadera and loadera-delegates-to-app.

Also note that there are no cycles in the Configuration guaranteed, but 
technically there is nothing to stop you having cycles in the loader 
delegation. I will show an example where this almost occurred in the JDK 
in my JavaOne talk, "Project Jigsaw: Under The Hood", which also covers 
the loaders-and-layers issues above.

Alex


More information about the jigsaw-dev mailing list