Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
JAVA9 load module in runtime
import java.lang.module.ModuleFinder;
import java.lang.module.ModuleReference;
import java.lang.reflect.Method;
import java.util.Optional;
public class LoadModule {
public static void main(String[] args) throws Exception {
public static void load(String name) throws Exception {
Optional<ModuleReference> ref = ModuleFinder.ofSystem().find(name);
ModuleLayer boot = ModuleLayer.boot();
Module m = boot.findModule("java.base").get();
ClassLoader scl = ClassLoader.getSystemClassLoader();
Method addOpens = m.getClass().getDeclaredMethod("implAddOpens", String.class);
addOpens.invoke(m, "jdk.internal.loader");
Method loadModule = scl.getClass().getMethod("loadModule", ModuleReference.class);
loadModule.invoke(scl, ref.get());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.