tibbe tibbe - 3 years ago 119
Java Question

What is the Java 9 reflection equivalent of --add-modules?

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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download