4

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 ?

  • Why do you want to load this at runtime? – christopher Sep 18 '18 at 07:52
  • 1
    Looking for something like this [Best approach to dynamically load modules (classes) in Java](https://stackoverflow.com/questions/48742354/best-approach-to-dynamically-load-modules-classes-in-java) ? – Naman Sep 18 '18 at 08:00
  • If this is JDK 9 or JDK 10 then the issue may be because javafx.controls is not in the boot layer. If you run with `--add-modules ALL-SYSTEM` or `--add-modules javafx.controls` then it will resolve javafx.controls. With JDK 11 then you'll put the JavaFX directory on your module path and it should just work. – Alan Bateman Sep 18 '18 at 09:56
  • Finally, I have found the error. My code works but not inside the IDE... –  Sep 18 '18 at 10:20
  • For answer at your question christopher, it's for built a plugin system. –  Sep 18 '18 at 12:14

0 Answers0