Chapter 11 Modules Notes Flashcards
Modules
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
EXPLORING A MODULE
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
BENEFITS OF MODULES
-
Better Access Control
- a 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.
Better Access Control
what if we wrote some complex logic that we wanted to restrict to just some packages?
- Modules solve this problem by acting as a 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.
- You’ll see how to code it when we talk about the module-info.java file later in this chapter.
Clearer Dependency Management
In a fully modular environment, each of the open source projects would specify their dependencies in the module-info.java file.
Custom Java Builds
- The Java Platform Module System allows developers to specify what modules they actually need.
- This makes it possible to create a smaller runtime image that is customized to what the application needs and nothing more.
- Users can run that image without having Java installed at all.
- A tool called jlink is used to create this runtime image. Luckily, you only need to know that custom smaller runtimes are possible. How to create them is out of scope for the exam.
In addition to the smaller scale package, this approach improves security. If you don’t use AWT and a security vulnerability is reported for AWT, applications that packaged a runtime image without AWT aren’t affected.
Improved Performance
Since Java now knows which modules are required, it only needs to look at those at class loading time. This improves startup time for big programs and requires less memory to run.
Unique Package Enforcement
Another manifestation of JAR hell is when the same package is in two JARs. There are a number of causes of this problem including renaming JARs, clever developers using a package name that is already taken, and having two versions of the same JAR on the classpath.
The Java Platform Module System prevents this scenario. A package is only allowed to be supplied by one module. No more unpleasant surprises about a package at runtime.
MODULES FOR EXISTING CODE
test
Creating and Running a Modular Program
-
CREATING THE FILES
-
class file
- Java classes should be in packages.
-
module-info.java
- module keyward
- the root directory of your module.
-
class file
- The next step is to make sure the files are in the right directory structure.
-
COMPILING OUR FIRST MODULE
javac --module-path mods -d feeding feeding/zoo/animal/feeding/*.java feeding/module-info.java
CREATING THE FILES
Create a class file:
package zoo.animal.feeding; public class Task { public static void main(String... args) { System.out.println("All fed!"); } }
module-info.java
file. This is the simplest possible one.
module zoo.animal.feeding { }
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
Options you need to know for using modules with javac
-
Directory for class files,
-d <dir>
-
Module path,
-p <path>, --module-path <path>
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
BUILDING MODULES
Do be sure to memorize the module command syntax. You will be tested on it on the exam. We will be sure to give you lots of practice questions on the syntax to reinforce it.
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
java --module-path feeding --module zoo.animal.feeding/zoo.animal.feeding.Task java -p feeding -m zoo.animal.feeding/zoo.animal.feeding.Task
--module-path uses the short form of -p
--module short option is -m
Options you need to know for using modules with java
-
Module name,
-m <name>, --module <name>
-
Module path,
-p <path>, --module-path <path>