Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Java 17. Manual Loading (loading and loadable with dependency)


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.

Solution


Modules (declared via module-info.java):

  • manual.fewmodules.separately.withdeps.loading:
    • Main - a class containing the main method, where an instance of the CustomClassLoader class is created, the Cat class is loaded using it, creating an instance of the class Cat and calling the Cat::talk method;
    • CustomClassLoader - a class that is an implementation of a custom class loader;
  • manual.fewmodules.separately.withdeps.loadable:
    • Cat - loadable class with the talk method, which prints the string "Meow" to stdout;
  • manual.fewmodules.separately.withdeps.dependency:
    • Dog - used in the module manual.fewmodules.separately.withdeps.loadable class with a talk method that prints the string "Woof" to stdout.

Run


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

Explanation


Java 17. Manual Loading (loading and loadable with dependency).jpg

Same as in Java 17. Manual Loading (dependent and dependency).