Java9+ - How to load a module at runtime?

Viewed 2485

I'm trying to load a module at runtime but it works badly. I have used the example found in the class ModuleLayer:

 ModuleFinder finder = ModuleFinder.of(Paths.get("out/artifacts/my_module/"));
 ModuleLayer parent = ModuleLayer.boot();
 Configuration cf = parent.configuration().resolve(finder, ModuleFinder.of(), Set.of("my.module"));
 ClassLoader scl = ClassLoader.getSystemClassLoader();
 ModuleLayer layer = parent.defineModulesWithOneLoader(cf, scl);
 Class<?> c = layer.findLoader("my.module").loadClass("main.java.org.myproject.MyClass");

The problem come from the requirements of the module I want to load. My IDE shows me this error:

Module javafx.controls not found, required by my.module

However, the module "javafx.controls" is written in the module descriptor:

module my.module {

    requires javafx.controls;

    exports main.java.org.myproject;
}

If I add manually javafx.controls in my main module descriptor, It works but I want to load a module without add manually his required module(s) !

Why "my.module" is not able to load javafx.controls ?

Anyone have the solution, please ?

0 Answers
Related