Sub topic 2
Modules in a Nutshell
This chapter assumes you can compile and run programs with javac and java, and that you know what a JAR file and the classpath are. It explains the Java Platform Module System (JPMS) well enough to understand what jlink does later in the tutorial. It does not cover every module feature.
Why modules exist
Before Java 9, the JDK was one large block. The classpath was a flat list of JARs. Any class on it could reach any public class on it, and a missing JAR showed up only at run time as a NoClassDefFoundError.
The module system, introduced in Java 9, addresses three problems:
- Reliable configuration. A module states which other modules it needs. The runtime checks this at startup instead of failing later.
- Strong encapsulation. A module chooses which packages other modules may use. A
publicclass in a package that is not exported is still inaccessible from outside the module. - A modular JDK. The JDK itself is split into modules, so a runtime can include only the parts an application uses.
The third point is what makes jlink possible. jlink can only select pieces of the JDK because the JDK has pieces.
What a module is
A module is a named set of packages, plus a descriptor that records its dependencies and what it exposes. The descriptor is a file named module-info.java at the root of the module's source. The compiler turns it into module-info.class, which sits at the root of the module's JAR.
A JAR with a module-info.class is a modular JAR. The same JAR placed on the classpath is treated as an ordinary JAR, and its descriptor is ignored.
The module descriptor
Here is the descriptor used throughout this tutorial:
module com.example.app {
requires java.logging;
}The module is named com.example.app. By convention the name matches the root package, in reverse-domain style. The requires directive states that this module needs java.logging, the JDK module that contains java.util.logging.
The two directives you will use most are these:
| Directive | Meaning |
|---|---|
| `requires M;` | This module reads module `M` and needs it at compile time and run time. |
| `exports P;` | Other modules may access the public types in package `P`. |
Two variants matter later. requires transitive M; makes anyone who requires your module also read M, which suits modules whose public API exposes types from M. exports P to X; limits access to a named module X.
Every module also reads java.base implicitly. It holds java.lang, java.util, java.io and the other core packages, and you never write requires java.base;. Every runtime image therefore contains java.base, whatever else you add.
Readability and accessibility
Two rules decide whether code in one module can use code in another:
- Readability. Module A must read module B. A reads B if A has a
requiresfor B, if B isjava.base, or if A reads it transitively. - Accessibility. The package must be exported by B, and the type must be
public.
Both must hold. If either fails, the compiler rejects the code. If code gets around the compiler with reflection, the runtime throws an exception.
The application class from our running example shows the first rule:
package com.example.app;
import java.util.logging.Logger;
public class Main {
private static final Logger LOG = Logger.getLogger(Main.class.getName());
public static void main(String[] args) {
LOG.info("Hello from a custom runtime");
}
}java.util.logging.Logger lives in java.logging. If you removed the requires java.logging; line, javac would report that the package java.util.logging is declared in module java.logging, which module com.example.app does not read. The error appears at compile time, which is the point of the design.
The module graph
Modules form a directed graph. Each module is a node, and each requires is an edge. At startup the runtime begins from one or more root modules, follows the requires edges, and builds the module graph of everything that is reachable. Modules that are not reachable are not loaded.
jlink uses the same idea. You name the root modules with --add-modules, and jlink resolves the graph from there. It then writes an image that contains exactly the modules in the resulting graph. The image for our example holds com.example.app, java.logging and java.base, and nothing else.
Modules in the JDK
You can see the JDK's modules yourself:
java --list-modulesOn a full JDK this prints several dozen entries, such as java.base, java.logging, java.sql, java.xml and jdk.compiler. Names starting with java. are the standard modules that are part of the Java SE specification. Names starting with jdk. are JDK-specific. To read one module's descriptor:
java --describe-module java.loggingThe output lists what the module exports and what it requires. This is useful when you want to know why a module appears in an image.
Modules and the module path
The classpath is for plain JARs. The module path is its counterpart for modules, and you set it with --module-path (or -p). Given a module path, the tools look for modules by name instead of scanning for classes.
To run a modular application, you name the main module and class:
java --module-path mods --module com.example.app/com.example.app.MainHere mods is a directory containing app.jar. The part before the slash is the module name, and the part after it is the class that holds main. If a dependency is missing, the runtime stops at startup with a message such as Module java.sql not found. This is the "reliable configuration" benefit in practice.
jlink also reads a module path, and for the JDK it uses a special format covered in the next chapters.
Unnamed and automatic modules
Not all code on your machine is modular, so the system has two bridging concepts. You only need the outline now.
- The unnamed module. Everything on the classpath is placed in a single unnamed module. It reads every other module, but a named module cannot declare a dependency on it.
- Automatic modules. A plain JAR placed on the module path becomes an automatic module. Its name comes from an
Automatic-Module-Namemanifest entry, or else from the file name. It exports all of its packages and reads all other modules.
jlink has a strict rule here: it refuses to link automatic modules. This is the main obstacle for applications that depend on older libraries, and a later chapter returns to it.
Strong encapsulation in practice
Before Java 9, code could use internal JDK classes such as sun.misc.Unsafe with little friction. Now packages that a JDK module does not export are inaccessible. From Java 17 onward, reflective access to those packages is blocked too, unless you pass --add-opens at launch.
This matters for runtimes because a library that quietly depends on a hidden JDK internal can work on a full JDK and fail on a trimmed image, or fail on both. Finding that out early is cheaper than finding out in production.
Common mistakes
- Putting a modular JAR on the classpath and expecting the module rules to apply. They do not. The descriptor is ignored.
- Forgetting that
requiresis not transitive by default. If module A requires B and B requires C, then A does not read C unless B writesrequires transitive C;. - Exporting too much. Exporting every package undoes the encapsulation benefit. Export only the API.
- Confusing the module name with the JAR name.
app.jarcan contain the modulecom.example.app. Tools that take module names (--add-modules,--module) need the second, not the first. - Assuming
requirespulls in a library. The directive names a module that must already be on the module path. It does not download anything.
Where this leads
You now have the vocabulary needed for the rest of the tutorial:
- A module has a name and a descriptor with
requiresandexports. - The runtime resolves a module graph from root modules.
- The JDK is a set of modules, and
java.baseis always present. jlinkbuilds an image from the resolved graph, so a precise descriptor leads to a precise image.
The next chapters go from this theory to tooling. You will see how jdeps reads descriptors and class files to list the modules an application needs, and how jlink turns that list into a runtime.
Exercise
A module com.example.report contains requires java.sql; and nothing else, and java.sql itself requires java.logging and java.xml. Which modules are in the resolved module graph if com.example.report is the only root module, and why does com.example.report still fail to compile if its code calls java.util.logging.Logger?
Show answer
The graph contains com.example.report, java.sql, java.logging, java.xml and java.base. The first three are reachable through requires edges, and java.base is read implicitly by every module. The code still fails to compile because requires is not transitive by default. com.example.report reads java.sql, but it does not read java.logging unless java.sql declares requires transitive java.logging; (and in the JDK it does, so this particular case compiles). In general, add requires java.logging; yourself whenever your own code uses its packages, rather than relying on another module's declarations.
Exercise
You put a modular JAR on the classpath with java -cp app.jar com.example.app.Main. Is the module descriptor enforced? What flag and argument form would run it as a module instead?
Show answer
No. On the classpath the JAR becomes part of the unnamed module and its module-info.class is ignored. To run it as a module, use java --module-path app.jar --module com.example.app/com.example.app.Main.