|
| 1 | +## Java 17. Auto Loading (dependent and dependency) |
| 2 | +--- |
| 3 | +**Task:** load the classes of an application consisting of two modules (the modules are linked via `module-info.java`) using a custom class loader (`CustomClassLoader`). |
| 4 | + |
| 5 | +### Solution |
| 6 | +--- |
| 7 | +Modules (declared via `module-info.java`): |
| 8 | +- `manual.fewmodules.together.dependent`: |
| 9 | + - [`Main`](dependent/src/ru/ispras/j17/manual/fewmodules/together/dependent/Main.java) - a class containing a `main` method creating an instance of the `Cat` class and calling the `Cat::talk` method; |
| 10 | + - [`CustomClassLoader`](dependent/src/ru/ispras/j17/manual/fewmodules/together/dependent/CustomClassLoader.java) - a class that is an implementation of a custom class loader; |
| 11 | +- `manual.fewmodules.together.dependency`: |
| 12 | + - [`Cat`](dependency/src/ru/ispras/j17/manual/fewmodules/together/dependency/Cat.java) - loadable class with the `talk` method, which prints the string *"Meow"* to `stdout`. |
| 13 | + |
| 14 | +To change the system loader to a custom `CustomClassLoader` in [documentation of the `ClassLoader.getSystemClassLoader()` method](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/ClassLoader.html#getSystemClassLoader()) it is written that for this it is necessary for the JVM to pass the name of the new *system class loader* through the argument `java.system.class.loader`, and also to define a public constructor with a parameter of type `ClassLoader`, which will be passed as `AppClassLoader` when created. |
| 15 | + |
| 16 | +### Run |
| 17 | +--- |
| 18 | +Using `modulepath`: |
| 19 | + |
| 20 | +```shell |
| 21 | +java17 -Djava.system.class.loader=ru.ispras.j17.auto.fewmodules.together.dependent.CustomClassLoader \ |
| 22 | +-p dependent-1.0.jar:dependency-1.0.jar -m auto.fewmodules.together.dependent |
| 23 | +``` |
| 24 | + |
| 25 | +Output: |
| 26 | + |
| 27 | +```shell |
| 28 | +Main Class Module is module auto.fewmodules.together.dependent |
| 29 | +Cat Class Module is module auto.fewmodules.together.dependency |
| 30 | +System ClassLoader is ru.ispras.j17.auto.fewmodules.together.dependent.CustomClassLoader@5a07e868 |
| 31 | +Main Class ClassLoader is jdk.internal.loader.ClassLoaders$AppClassLoader@42a57993 |
| 32 | +Cat Class ClassLoader is jdk.internal.loader.ClassLoaders$AppClassLoader@42a57993 |
| 33 | +Meow |
| 34 | +``` |
| 35 | + |
| 36 | +### Explanation |
| 37 | +--- |
| 38 | +.jpg) |
| 39 | + |
| 40 | +- Regarding [Java 17. Auto Loading (Java 17 style)](../../one-module/java17style) the only difference is that there are two |
| 41 | + modules that |
| 42 | + are |
| 43 | + loaded using `AppClassLoader`. |
| 44 | + |
| 45 | +### Notes |
| 46 | +--- |
| 47 | +*Same as in [Java 17. Auto Loading (Java 17 style)](../../one-module/java17style)*. |
| 48 | + |
| 49 | +- In `module-info.java` of the `dependency` module you need to export the package with the `Cat` class, and in `module-info.java` of the `dependent` module, specify the dependency on the `dependency` module and export the custom class loader `CustomClassLoader`: |
| 50 | + |
| 51 | +```java |
| 52 | +// dependency/src/module-info.java |
| 53 | +module auto.fewmodules.together.dependency { |
| 54 | + exports ru.ispras.j17.auto.fewmodules.together.dependency; |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +```java |
| 59 | +// dependent/src/module-info.java |
| 60 | +module.auto.fewmodules.together.dependent { |
| 61 | + requires auto.fewmodules.together.dependency: |
| 62 | + |
| 63 | + exports ru.ispras.j17.auto.fewmodules.together.dependent to java.base; |
| 64 | +} |
| 65 | +``` |
0 commit comments