Chapter 11 Modules Flashcards
Understanding Modules * Describe the Modular JDK * Declare modules and enable access between modules * Describe how a modular project is compiled and run
Introducing Modules
- The Java Platform Module System (JPMS) was introduced in Java 9
- The main purpose of a module is to provide groups of related packages to offer a particular set of functionality to developers.
The Java Platform Module System includes the following:
- A format for module JAR files
- Partitioning of the JDK into modules
- Additional command-line options for Java tools
A module
is a group of one or more packages
plus a special file called module-info.java
.
dependencies where one module relies on code in another.
Java Platform Module System (JPMS)
- The Java Platform Module System (JPMS) was introduced in Java 9
- The Java Platform Module System includes the following:
- A format for module JAR files
- Partitioning of the JDK into modules
- Additional command-line options for Java tools
BENEFITS OF MODULES
-
Better Access Control
- fifth level of access control.
- They can expose packages within the modular JAR to specific other packages.
- This stronger form of encapsulation really does create internal packages.
-
Clearer Dependency Management
specify their dependencies in the module-info.java file. -
Custom Java Builds
- allows developers to specify what modules they actually need.
- A tool called
jlink
is used to create this runtime image.
-
Improved Performance
improves startup time and requires less memory to run. -
Unique Package Enforcement
A package is only allowed to be supplied by one module.
module-info.java
This is the simplest possible module-info.java
file
module zoo.animal.feeding { }
- The module-info file must be in the root directory of your module.
- The module-info file must use the keyword module.
1: module zoo.animal.care { 2: exports zoo.animal.care.medical; 3: requires zoo.animal.feeding; 4: }
- Line 1 specifies the name of the module.
- Line 2 lists the package we are exporting so it can be used by other modules.
- On line 3, we see a new keyword. The requires statement specifies that a module is needed. The zoo.animal.care module depends on the zoo.animal.feeding module.
There are a few key differences between a module-info
file and a regular Java class
:
- The
module-info
file must be in the root directory of your module. Regular Java classes should be in packages. - The
module-info
file must use the keywordmodule
instead of class, interface, or enum. - The module name follows the naming rules for package names. It often includes periods (.) in its name. Regular class and package names are not allowed to have dashes (-). Module names follow the same rule.
That’s a lot of rules for the simplest possible file. There will be many more rules when we flesh out this file later in the chapter.
CAN A MODULE-INFO.JAVA
FILE BE EMPTY?
Yes. As a bit of trivia,
it was legal to compile any empty file with a .java extension even before modules.
The compiler sees there isn’t a class in there and exits without creating a .class file.
COMPILING OUR FIRST MODULE
> [!NOTE]
When you’re entering commands at the command line, they should be typed all on one line.
javac --module-path mods -d feeding feeding/zoo/animal/feeding/*.java feeding/module-info.java
- the
-d
option specifies the directory to place the class files in. - The end of the command is a list of the .java files to compile. You can list the files individually or use a wildcard for all .java files in a subdirectory.
-
--module-path
option indicates the location of any custom module files. - The syntax
--module-path
and-p
are equivalent.
The following four commands show the -p option:
javac -p mods -d feeding feeding/zoo/animal/feeding/*.java feeding/*.java javac -p mods -d feeding feeding/zoo/animal/feeding/*.java feeding/module-info.java javac -p mods -d feeding feeding/zoo/animal/feeding/Task.java feeding/module-info.java javac -p mods -d feeding feeding/zoo/animal/feeding/Task.java feeding/*.java
WHAT HAPPENED TO THE CLASSPATH
?
You can still use these options in Java 11. In fact, it is common to do so when writing nonmodular programs.
-cp, --class-path, and -classpath
RUNNING OUR FIRST MODULE
Pay special attention to the book.module/com.sybex.OCP part. It is important to remember that you specify the module name followed by a slash (/) followed by the fully qualified class name.
java --module-path mods --module book.module/com.sybex.OCP
-
Location of modules,
--module-path, -p
-
Module name,
--module, -m
- Module/package separator
- Package name
- Class name
examples:
java --module-path feeding --module zoo.animal.feeding/zoo.animal.feeding.Task java -p feeding -m zoo.animal.feeding/zoo.animal.feeding.Task
PACKAGING OUR FIRST MODULE
Be sure to create a mods directory before running this command:
jar -cvf mods/zoo.animal.feeding.jar -C feeding/ .
- We are packaging everything under the feeding directory
- and storing it in a JAR file named zoo.animal.feeding.jar under the mods folder.
run the program again, but this time using the mods directory instead of the loose classes:
java -p mods -m zoo.animal.feeding/zoo.animal.feeding.Task
Since the module path is used, a module JAR is being run.
> [!NOTE]
It is possible to version your module using the –module-version option.
This isn’t on the exam but is good to do when you are ready to share your module with others.
version your module using the –module-version option.
ORDER MATTERS!
Order matters when compiling a module.
javac -p mods -d care care/module-info.java care/zoo/animal/care/details/*.java care/zoo/animal/care/medical/*.java
The compiler complains that it doesn’t know anything about the package zoo.animal.care.medical.
care/module-info.java:3: error: package is empty or does not exist: zoo.animal.care.medical exports zoo.animal.care.medical;
- A package must have at least one class in it in order to be exported.
- Since we haven’t yet compiled zoo.animal.care.medical.Diet, the compiler acts as if it doesn’t exist.
- If you get this error message, you can reorder the javac statement.
- Alternatively, you can compile the packages in a separate javac command, before compiling the module-info file.
jar -cvf mods/zoo.animal.care.jar -C care/ .
Diving into the module-info File
These are not Java keywords they are directives, can appear in any order in the module-info file.
- exports,
- requires,
- provides,
- uses,
- and opens.
EXPORTS
- exports packageName exports a package to other modules.
- It’s also possible to export a package to a specific module.
module zoo.animal.talks { exports zoo.animal.talks.content to zoo.staff; exports zoo.animal.talks.media; exports zoo.animal.talks.schedule; requires zoo.animal.feeding; requires zoo.animal.care; }
EXPORTED TYPES
Exporting a package
- All public classes, interfaces, and enums are exported.
- Further, any public and protected fields and methods in those files are visible.
- Fields and methods that are private are not visible because they are not accessible outside the class.
- Similarly, package-private fields and methods are not visible because they are not accessible outside the package.