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 themain
method, where an instance of theCustomClassLoader
class is created, theCat
class is loaded using it, creating an instance of the classCat
and calling theCat::talk
method;CustomClassLoader
- a class that is an implementation of a custom class loader;
manual.fewmodules.separately.withdeps.loadable
:Cat
- loadable class with thetalk
method, which prints the string "Meow" tostdout
;
manual.fewmodules.separately.withdeps.dependency
:Dog
- used in the modulemanual.fewmodules.separately.withdeps.loadable
class with atalk
method that prints the string "Woof" tostdout
.
Using modulepath
:
java17 -p . -m manual.fewmodules.separately.withdeps.loading
Output:
[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
Woof
Same as in Java 17. Manual Loading (dependent and dependency).