The Java Platform Module System

mug

Part 1: Basic Concepts

The Module Concept

Module Names

The Modular Hello, World!

The Modular Hello, World!

mod1

javac mod1/module-info.java mod1/com/horstmann/hello/HelloWorld.java
java --module-path ch15.sec03 --module mod1/com.horstmann.hello.HelloWorld

or

java -p mod1 -m mod1/com.horstmann.hello.HelloWorld

Exporting Packages

Requiring Modules

mod2

javac mod2/module-info.java mod2/com/horstmann/hello/HelloWorld.java

fails.

Add

requires java.desktop;

to mod2/module-info.java

java -p mod2 -m mod2/com.horstmann.hello.HelloWorld

Module Graph

modulegraph

Part 2: Runtime Access

Reflection

mod3

javac mod3/module-info.java mod3/com/horstmann/places/*.java
java -p mod3 -m mod3/com.horstmann.places.Demo

To fix, add

opens com.horstmann.places;

to mod3/module-info.java

Open Modules, Qualified Opening

Service Loaders

ServiceLoader

Service Loaders

Part 3: Migration and Troubleshooting

Towards a Modular World

The Unnamed Module

More Compatibility Options

mod4

All works well on the class path:

javac -classpath mod4:mod4/commons-csv-1.4.jar mod4/com/horstmann/places/CSVDemo.java
java -classpath mod4:mod4/commons-csv-1.4.jar com.horstmann.places.CSVDemo 

But this doesn't work:

javac -classpath mod4:mod4/commons-csv-1.4.jar \
   mod4/com/horstmann/places/CSVDemo.java mod4/module-info.java

Now add the JAR file as an automatic module to the module path.

javac --module-path mod4:mod4/commons-csv-1.4.jar \
   mod4/com/horstmann/places/CSVDemo.java b mod4/module-info.java
  

Fix by adding

requires commons.csv;

to mod4/module-info.java

java --module-path mod4:mod4/commons-csv-1.4.jar \
   -m mod4/com.horstmann.places.CSVDemo