I need a way to tell a newly created class loader (with no set parent) about modules I want exposed to it (e.g. java.scripting
).
What's the reflection equivalent of passing --add-modules
on the command line?
I need a way to tell a newly created class loader (with no set parent) about modules I want exposed to it (e.g. java.scripting
).
What's the reflection equivalent of passing --add-modules
on the command line?
There is no programmatic way to substantially edit the module graph created by the JVM on launch (adding reads edges is the only exception). This was a deliberate decision to keep the running application secure and stable. (Case in point, what would happen to your code, if you ran on a runtime that doesn't contain the java.scripting module?)
What you can do, though, is create a new layer, which contains an entirely new module graph. When launching the JVM, it will create a single layer from the command line flags and the module path content. With the existing API, you can then create new layers on top of that one. To learn about layers, have a look at The State of the Module System and the Javadoc for ModuleLayer
.
--add-modules=java.scripting
when I start the app java.scripting
classes fail to be looked-up by classes loaded through my new ClassLoader
object. Isn't exposed modules inherited between the default and new class loaders? –
Daphnedaphnis © 2022 - 2024 — McMap. All rights reserved.
--add-opens
, e.g.--add-opens java.base/java.util=ALL-UNNAMED
– Prochora