Cannot use a mapper function to get the Platform classloader

Alan Bateman Alan.Bateman at oracle.com
Sun Dec 13 16:00:31 UTC 2020


On 12/12/2020 15:05, Thiago Henrique Hupner wrote:
> Hi.
>
> I'm playing around with layers and I needed the following:
> Create a new layer where it doesn't have access to the boot layer using the
> ModuleLayer.empty().
> However, I'm getting the exception that java.base is not found. So I used
> something like this:
>
> ModuleLayer.boot().defineModules(conf, moduleName ->
> myNewLayer.find(moduleName).isPresent() ? myClassLoader :
> ModuleFinder.ofSystem().find(moduleName).isPresent() ?
> ClassLoader.getPlatformClassLoader() : null).
>
> Then I get the following exception: java.lang.LayerInstantiationException:
> loader can't be 'null' or the platform class loader.
>
> Why is there this restriction? How is the correct way of only enabling the
> base modules to be found to the next layer and not the unnamed module.
java.base is in the boot layer and any layer you create will ultimately 
have the boot layer as its parent. If I read your mail correctly then it 
sounds like you want to restrict modules so that the only standard 
module that they read is java.base, is that correct? In that case, the 
simplest is to run with `--limit-modules java.base` so that the only 
module in the boot layer is java.base. Alternatively, maybe you should 
start with a minimum configuration like this:

         Configuration minConfiguration = Configuration.empty().
                 resolve(ModuleFinder.ofSystem(), ModuleFinder.of(), 
Set.of("java.base"));

and use that as the parent configuration. If this minimum configuration 
works as the parent configuration, meaning resolve or resolveAndBind 
succeeds, then you can be assured that there are aren't any observable 
modules that require other standard modules. A scan of the observable 
modules to examine their dependences will also suffice. Then just 
ModuleLayer.boot().configuration() as the parent when creating the 
configuration for the child parent. Does that help?

-Alan



More information about the jigsaw-dev mailing list