Modules
Modules
Java 11 (1Z0-819)
Farm Application
farm.owner Modules
farm.animals farm.count
farm.vet farm.sale
farm.animals Module
Cattle.java
Animal.java
Cow.java
Listing all the source files: javac –d {dir} {all the java source files, including module-info.java}
Module directory(ies): javac –d {dir} –m {module_name}, {module_name} --module-source-path {src_dir}
Dependencies to other --module-path or –p
custom (possibly 3rd party)
modules:
Examples: javac -d out src/com/farm/Main.java src/module-info.java
javac -d out -m TestModuleFarm --module-source-path src
javac -d out --module TestModuleFarm --module-source-path src
javac –d {dir} –m {module_name} --module-source-path {src_dir} –p {dir e.g. “mods”}
12
16
• Remember:
• you export (exports) a package
• you depend (requires) on a module
17
farm.stock.sale farm.stock.count
21
farm.vet farm.owner
farm.animals
22
farm.vet farm.owner
farm.animals
Note: The following is an error:
23
• jmod
• native libraries
• not executable
• intended for use with the jlink tool to build a custom native
image.
24
farm.animals
26
farm.neighbour
farm.inspector farm.hired_hand
farm.spouse
farm.family_member
27
provides
farm.vet
farm.neighbour
Service Contract
owner.api
softdrink.customer
softdrink.medium
Registry
softdrink.large
Service Contract
softdrink.extralarge
softdrink.api
32
33
Bottom-up Top-down
application.jar
utils.jar other.jar
data-storage.jar services.jar
34
• Algorithm:
1. Start at the “leaf” nodes i.e. nodes that have no dependencies
other than the JDK.
2. Create a module-info.java file for the JAR. Name the module.
Ensure all packages required by other (higher-level) JAR’s are
exported using the exports directive. In addition, all modules this
JAR depends upon must have requires directives.
3. Once migrated, this newly named module moves from the
classpath to the module path.
4. Repeat with the next lowest JAR until you finally have
modularised the top level JAR i.e. you are done.
35
• So, rather than having to analyse all the code to check for all
dependencies, jdeps can do that for you.
• All we need to know is what name Java will give the automatic
module and modify our dependencies (requires) accordingly.
38
unnamed unnamed
external-utils.jar some-application.jar
classpath
module-path
external.utils some.application
automatic named
39
Java 11 (1Z0-819)
40