Introducing Modules
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
jlink is used to create this runtime image.Better Access Control
what if we wrote some complex logic that we wanted to restrict to just some packages?
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
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
javac --module-path mods -d feeding feeding/zoo/animal/feeding/*.java feeding/module-info.javaCREATING 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:
module-info file must be in the root directory of your module. Regular Java classes should be in packages.module-info file must use the keyword module instead of class, interface, or enum.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
-d option specifies the directory to place the class files in.--module-path option indicates the location of any custom module files.--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
-d <dir>-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
--module-path, -p--module, -mjava --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 -mOptions you need to know for using modules with java
-m <name>, --module <name>-p <path>, --module-path <path>PACKAGING OUR FIRST MODULE
Be sure to create a mods directory before running this command:
jar -cvf mods/zoo.animal.feeding.jar -C feeding/ .
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.
Updating Our Example for Multiple Modules
test
UPDATING THE FEEDING MODULE
other modules call code in the zoo.animal.feeding package, we need to declare this intent in the module-info file.
The exports keyword is used to indicate that a module intends for those packages to be used by Java code outside the module. As you might expect, without an exports keyword, the module is only available to be run from the command line on its own. In
module zoo.animal.feeding {
exports zoo.animal.feeding;
}Recompiling and repackaging the module will update the module-info inside our zoo.animal.feeding.jar file. These are the same javac and jar commands you ran previously.
javac -p mods -d feeding feeding/zoo/animal/feeding/*.java feeding/module-info.java
jar -cvf mods/zoo.animal.feeding.jar -C feeding/ .