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?
Defining the Java 9 module. A module is a collection of code, data, and resources. It is a set of related packages and types (classes, abstract classes, interfaces, and more) with code, data files, and some static resources. For example, the module descriptor module-info.
Java Module System is a major change in Java 9 version. Java added this feature to collect Java packages and code into a single unit called module. In earlier versions of Java, there was no concept of module to create modular Java applications, that why size of application increased and difficult to move around.
A Java module is a packaging mechanism that enables you to package a Java application or Java API as a separate Java module. A Java module is packaged as a modular JAR file. A Java module can specify which of the Java packages it contains that should be visible to other Java modules which uses this module.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With