Skip to content

Commit 6af156b

Browse files
committed
[J17] Add intermodule class loading example description
1 parent 99fac77 commit 6af156b

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
## Java 17. Manual Loading (loading and loadable with dependency)
2+
---
3+
**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`.
4+
5+
### Solution
6+
---
7+
Modules (declared via `module-info.java`):
8+
- `manual.fewmodules.separately.withdeps.loading`:
9+
- [`Main`](loading/src/ru/ispras/j17/manual/fewmodules/separately/nodeps/loading/Main.java) - 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;
10+
- [`CustomClassLoader`](loading/src/ru/ispras/j17/manual/fewmodules/separately/nodeps/loading/CustomClassLoader.java) - a class that is an implementation of a custom class loader;
11+
- `manual.fewmodules.separately.withdeps.loadable`:
12+
- [`Cat`](loadable/src/ru/ispras/j17/manual/fewmodules/separately/nodeps/loadable/Cat.java) - loadable class with the `talk` method, which prints the string *"Meow"* to `stdout`;
13+
- `manual.fewmodules.separately.withdeps.dependency`:
14+
- [Dog](dependency/src/ru/ispras/j17/manual/fewmodules/separately/withdeps/dependency/Dogjava) - used in the module `manual.fewmodules.separately.withdeps.loadable` class with a `talk` method that prints the string *"Woof"* to `stdout`.
15+
16+
### Run
17+
---
18+
Using `modulepath`:
19+
20+
```shell
21+
java17 -p . -m manual.fewmodules.separately.withdeps.loading
22+
```
23+
24+
Output:
25+
26+
```shell
27+
[loading] Main Class Module is module manual.fewmodules.separately.withdeps.loading
28+
[loading] Main Class ClassLoader is jdk.internal.loader.ClassLoaders$AppClassLoader@5c647e05
29+
ClassLoader for [loadable] module is jdk.internal.loader.Loader@30f39991
30+
[loadable] Main Class ClassLoader is jdk.internal.loader.Loader@30f39991
31+
[loadable] Cat Class ClassLoader is jdk.internal.loader.Loader@30f39991
32+
[dependency] Dog Class ClassLoader is jdk.internal.loader.Loader@30f39991
33+
[loadable] Main Class Module is module manual.fewmodules.separately.withdeps.loadable
34+
[loadable] Cat Class Module is module manual.fewmodules.separately.withdeps.loadable
35+
[dependency] Dog Class Module is module manual.fewmodules.separately.withdeps.dependency
36+
Meow
37+
Woof
38+
```
39+
40+
### Explanation
41+
---
42+
![Java 17. Manual Loading (loading and loadable with dependency).jpg](../../../../../img/Java%2017.%20Manual%20Loading%20(loading%20and%20loadable%20with%20dependency).jpg)
43+
44+
*Same as in [Java 17. Manual Loading (dependent and dependency)](https://github.com/temikfart/java-classloading/tree/main/java-17/manual/few-modules/together)*.

0 commit comments

Comments
 (0)