A layer of modules in the Java virtual machine.
A layer is created from a graph of modules that is the Configuration
and a function that maps each module to a ClassLoader
. Creating a layer informs the Java virtual machine about the classes that may be loaded from modules so that the Java virtual machine knows which module that each class is a member of. Each layer, except the empty
layer, has a parent
.
Creating a layer creates a Module
object for each ResolvedModule
in the configuration. For each resolved module that is read
, the Module
reads
the corresponding run-time Module
, which may be in the same layer or a parent layer. The Module
exports
the packages described by its ModuleDescriptor
.
The defineModulesWithOneLoader
and defineModulesWithManyLoaders
methods provide convenient ways to create a Layer
where all modules are mapped to a single class loader or where each module is mapped to its own class loader. The defineModules
method is for more advanced cases where modules are mapped to custom class loaders by means of a function specified to the method.
A Java virtual machine has at least one non-empty layer, the boot
layer, that is created when the Java virtual machine is started. The system modules , includingboot layer contains module java.base
, areand is the only layer in the boot layerJava virtual machine with a module named "java.base
". The modules in the boot layer are mapped to the bootstrap class loader and other class loaders that are built-in into the avaJava virtual machine. The boot layer will often be the parent
when creating additional layers.
As when creating a Configuration
, automatic
modules receive special treatment when creating a layer. An automatic module is created in the Java virtual machine as a Module
that reads every unnamed Module
in the Java virtual machine.
Unless otherwise specified, passing a null
argument to a method in this class causes a NullPointerException
to be thrown.
Example usage:
This example creates a configuration by resolving a module named "myapp
" with the configuration for the boot layer as the parent. It then creates a new layer with the modules in this configuration. All modules are defined to the same class loader.
ModuleFinder finder = ModuleFinder.of(dir1, dir2, dir3);
Layer parent = Layer.boot();
Configuration cf = parent.configuration()
.resolveRequires(finder, ModuleFinder.emptyof(), Set.of("myapp"));
ClassLoader scl = ClassLoader.getSystemClassLoader();
Layer layer = parent.defineModulesWithOneLoader(cf, scl);
Class<?> c = layer.findLoader("myapp").loadClass("app.Main");