Task: load any application class from another module (modules are NOT linked through module-info.java) using a custom class loader (CustomClassLoader). A loadable class module has a dependency described via module-info.java.
Modules (declared via module-info.java):
manual.fewmodules.separately.withdeps.loading:Main- a class containing themainmethod, where an instance of theCustomClassLoaderclass is created, theCatclass is loaded using it, creating an instance of the classCatand calling theCat::talkmethod;CustomClassLoader- a class that is an implementation of a custom class loader;
manual.fewmodules.separately.withdeps.loadable:Cat- loadable class with thetalkmethod, which prints the string "Meow" tostdout;
manual.fewmodules.separately.withdeps.dependency:Dog- used in the modulemanual.fewmodules.separately.withdeps.loadableclass with atalkmethod that prints the string "Woof" tostdout.
Using modulepath:
java17 -p . -m manual.fewmodules.separately.withdeps.loadingOutput:
[loading] Main Class Module is module manual.fewmodules.separately.withdeps.loading
[loading] Main Class ClassLoader is jdk.internal.loader.ClassLoaders$AppClassLoader@5c647e05
ClassLoader for [loadable] module is jdk.internal.loader.Loader@30f39991
[loadable] Main Class ClassLoader is jdk.internal.loader.Loader@30f39991
[loadable] Cat Class ClassLoader is jdk.internal.loader.Loader@30f39991
[dependency] Dog Class ClassLoader is jdk.internal.loader.Loader@30f39991
[loadable] Main Class Module is module manual.fewmodules.separately.withdeps.loadable
[loadable] Cat Class Module is module manual.fewmodules.separately.withdeps.loadable
[dependency] Dog Class Module is module manual.fewmodules.separately.withdeps.dependency
Meow
WoofSame as in Java 17. Manual Loading (dependent and dependency).
.jpg)