Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 651
tibbe Avatar asked Jun 07 '17 05:06

tibbe


People also ask

What are modules in java 9?

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.

Which is correct about module system in Java 9?

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.

What are modules in java project?

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.


1 Answers

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.

like image 72
Nicolai Parlog Avatar answered Sep 28 '22 03:09

Nicolai Parlog