Task: load any application class using a custom class loader (CustomClassLoader
) from another module (the modules are linked via module-info.java
).
Modules (declared via module-info.java
):
manual.fewmodules.together.dependent
: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.together.dependency
:Cat
- loadable class with thetalk
method, which prints the string "Meow" tostdout
.
Using modulepath
:
java17 -p dependent-1.0.jar:dependency-1.0.jar -m manual.fewmodules.together.dependent
Output:
Main Class Module is module manual.fewmodules.together.dependent
Cat Class Module is unnamed module @23fc625e
Main Class ClassLoader is jdk.internal.loader.ClassLoaders$AppClassLoader@42a57993
Cat Class ClassLoader is ru.ispras.j17.manual.fewmodules.together.dependent.CustomClassLoader@3fee733d
Meow
Same as in Java 17. Manual Loading (Java 17 style).
Same as in Java 17. Manual Loading (Java 17 style).
- In
module-info.java
of thedependency
module you need to export the package with theCat
class, and inmodule-info.java
of thedependent
module you need to specify the dependency on thedependency
module:
// dependency/src/module-info.java
module manual.fewmodules.together.depedency {
exports ru.ispras.j17.manual.fewmodules.together.dependency;
}
// dependent/src/module-info.java
module manual.fewmodules.together.dependent {
requires manual.fewmodules.together.dependency;
}