Best approach to dynamically load modules (classes) in Java

It sounds like you might want to use the ServicerLoader interface, which has been available since Java 6. However, bear in mind that, if you want to use Spring dependency injection, this is probably not what you want.


Additionnaly to the ServicerLoader usage given by @SeverityOne, you can use the module-info.java to declare the different instanciation of the interface, using "uses"/"provides" keywords.

Then you use a module path instead of a classpath, it loads all the directory containing your modules, don't need to create a specific classLoader

The serviceLoader usage:

public static void main(String[] args) {
    ServiceLoader<IGreeting> sl = ServiceLoader.load(IGreeting.class);
    IGreeting greeting = sl.findFirst().orElseThrow(NullPointerException::new);
    System.out.println( greeting.regular("world"));
}

In the users project:

module pl.tfij.java9modules.app {
    exports pl.tfij.java9modules.app;
    uses pl.tfij.java9modules.app.IGreeting;
}

In the provider project:

module pl.tfij.java9modules.greetings {
    requires pl.tfij.java9modules.app;
    provides pl.tfij.java9modules.app.IGreeting
            with pl.tfij.java9modules.greetings.Greeting;
}

And finally the CLI usage

java --module-path mods --module pl.tfij.java9modules.app

Here is an example; Github example (Thanks for "tfij/" repository initial exemple)

Edit, I realized the repository already provides decoupling examples: https://github.com/tfij/Java-9-modules---reducing-coupling-of-modules


There are two scenarios.

  1. Implementation jar's are on classpath
    In this scenario you can simply use ServiceLoader API (refer to @pdem answer)
  2. Implementation jar's not on classpath Lets Assume BankController is your interface and CoreController is your implementation.
    If you want to load its implementation dynamically from dynamic path,c create a new module layer and load class.

Refer to the following piece of code:

        private final BankController loadController(final BankConfig config) {
            System.out.println("Loading bank with config : " + JSON.toJson(config));
            try {
                //Curent ModuleLayer is usually boot layer. but it can be different if you are using multiple layers
                ModuleLayer currentModuleLayer       = this.getClass().getModule().getLayer(); //ModuleLayer.boot();
                final Set<Path> modulePathSet        = Set.of(new File("path of implementation").toPath());
                //ModuleFinder to find modules 
                final ModuleFinder moduleFinder      = ModuleFinder.of(modulePathSet.toArray(new Path[0]));
                //I really dont know why does it requires empty finder.
                final ModuleFinder emptyFinder       = ModuleFinder.of(new Path[0]);
                //ModuleNames to be loaded
                final Set<String>  moduleNames       = moduleFinder.findAll().stream().map(moduleRef -> moduleRef.descriptor().name()).collect(Collectors.toSet());
                // Unless you want to use URLClassloader for tomcat like situation, use Current Class Loader 
                final ClassLoader loader             = this.getClass().getClassLoader();
                //Derive new configuration from current module layer configuration
                final Configuration  configuration   = currentModuleLayer.configuration().resolveAndBind(moduleFinder, emptyFinder, moduleNames);
                //New Module layer derived from current modulee layer 
                final ModuleLayer    moduleLayer     = currentModuleLayer.defineModulesWithOneLoader(configuration, loader);
                //find module and load class Load class 
                final Class<?>       controllerClass = moduleLayer.findModule("org.util.npci.coreconnect").get().getClassLoader().loadClass("org.util.npci.coreconnect.CoreController");
                //create new instance of Implementation, in this case org.util.npci.coreconnect.CoreController implements org.util.npci.api.BankController
                final BankController bankController  = (BankController) controllerClass.getConstructors()[0].newInstance(config);
                return bankController;
            } catch (Exception e) {BootLogger.info(e);}
            return null;
        }