Sub topic 2
Modules in a nutshell
This chapter covers the Java Platform Module System (JPMS) as far as jlink needs it. It assumes you can compile and run programs with javac and java, use the command line, and know what a JAR and the classpath are.
Why jlink needs modules
jlink builds a runtime image from modules, not from loose classes or classpath JARs. To use it, you need three ideas: what a module is, how modules depend on each other, and how the JDK itself is split into modules.
A module is a named group of packages, plus a descriptor that states what the module needs and what it offers. Since Java 9, the JDK is built from modules. A runtime that includes only the modules you need is smaller than a full JDK.
Classpath versus module path
The classpath is a flat list of JARs. The runtime searches it in order, and nothing checks that the classes your code needs are present until it runs. Any public class in any JAR is visible to all code.
The module path works differently:
- Each module has a name.
- Each module declares its dependencies.
- The runtime checks at startup that every required module is present.
- A package is visible to other modules only if its module exports it.
Because the dependencies are declared, a tool can read them and compute exactly which modules an application needs. jlink does this.
The module descriptor
A module declares itself in a file named module-info.java at the root of its source tree. The compiler turns it into module-info.class.
module com.example.app {
requires java.logging;
}Two directives matter most:
requiresnames a module this module depends on.exportsmakes one of this module's packages available to other modules.
Every module implicitly requires java.base. It holds core packages such as java.lang and java.util, so you never write it yourself.
A library module that offers an API would add an exports line:
module com.example.library {
exports com.example.library.api;
}Packages that are not exported stay hidden, even if their classes are public. This is called strong encapsulation.
How the dependency graph is resolved
At startup, or when jlink runs, the module system begins with one or more root modules. It follows each requires directive and adds the required modules to the set. The result is the module graph.
jlink uses the same resolution. If you give it your application module as the root, it includes your module, every module it requires directly, and everything those modules require in turn. Nothing else goes into the image.
A missing module is an error at resolution time. A classpath application would instead fail later with a NoClassDefFoundError.
Common errors
- Missing
requires. Your code importsjava.util.logging.Loggerbut the descriptor lacksrequires java.logging;. The compiler reports that the package is not visible. - Wrong module name. Module names are not package names. The package
java.util.loggingbelongs to the modulejava.logging. - Expecting reflection to bypass encapsulation. Reflective access to a non-exported package fails with an
InaccessibleObjectException.
Checking a module's descriptor
You can inspect any module from the command line. This prints the descriptor of java.logging:
java --describe-module java.loggingThe output lists the modules it requires and the packages it exports. You will use the same technique later to see which modules an image contains.
Exercise
Your module uses java.sql.Connection, but the compiler says the package is not visible. What single line do you add to module-info.java, and why does it also affect the jlink image?
Show answer
Add requires java.sql;. The module system resolves the graph from the requires directives, and jlink uses that same graph to decide which modules go into the image. Without the directive the code does not compile, and with it jlink includes java.sql and the modules it requires.