Sub topic 3
Declaring dependencies with module-info.java
This chapter assumes you can compile and run Java programs with javac and java, and that you know what a JAR and the classpath are. It covers the file that tells the module system, and later jlink, what your code needs.
What a module descriptor is
A module is a named group of packages. Its descriptor is a file called module-info.java that sits at the root of the module's source tree. The compiler turns it into module-info.class, which ends up at the root of the modular JAR.
The descriptor answers two questions:
- Which other modules does this module need? These are the
requiresdirectives. - Which of its packages may other modules use? These are the
exportsdirectives.
jlink reads the first answer. When you ask it to build an image around your module, it follows the requires directives to decide which JDK modules to include.
A minimal descriptor
This 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 of the code, in reverse-domain style. The single directive says the module needs java.logging, the JDK module that contains java.util.logging.
The class below depends on that module. It uses java.util.logging.Logger:
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");
}
}Remove the requires line and compilation fails, because the package java.util.logging is not visible to the module. This is the main behavioral change from the classpath. A module sees only the modules it names, plus java.base.
The implicit dependency on java.base
Every module reads java.base, which holds java.lang, java.util, java.io, java.nio, and the other core packages. You never write requires java.base;. The compiler adds it for you. You may write it explicitly, but it adds nothing.
This has a consequence for jlink. An application that only uses core packages needs just java.base, and its image is the smallest possible one.
The requires directive
A requires directive names a module, not a package or a JAR. The module system checks these names at compile time and again at startup. If a required module is missing, the program fails before main runs, with an error such as Module java.sql not found, required by com.example.app.
The directive has two optional modifiers.
requires transitive
requires transitive makes the dependency visible to every module that requires yours. Use it when your module's public API exposes types from the other module.
module com.example.data {
requires transitive java.sql;
exports com.example.data;
}Suppose com.example.data has a public method that returns java.sql.Connection. Any module that calls it needs to see java.sql as well. With transitive, a module that writes requires com.example.data; gets java.sql automatically. Without it, each consumer would have to add requires java.sql; itself.
The JDK uses this pattern. java.sql declares requires transitive java.logging, and also java.xml and java.transaction.xa. If your module requires java.sql, it can use java.util.logging without naming java.logging. For jlink this means requiring java.sql also pulls in java.logging, java.xml, and java.transaction.xa.
requires static
requires static means the module is needed to compile but is optional at run time. It is common for annotation libraries.
module com.example.tools {
requires static java.compiler;
}Be careful here. jlink does not include a module that is only required with static, unless some other module requires it normally or you add it with --add-modules. If your code touches the optional module at run time without checking that it is present, you get a NoClassDefFoundError in the image.
The next table summarizes the three forms.
| Form | Needed at compile time | Needed at run time | Visible to your consumers |
|---|---|---|---|
| `requires m;` | Yes | Yes | No |
| `requires transitive m;` | Yes | Yes | Yes |
| `requires static m;` | Yes | No (optional) | No |
The exports directive
exports makes the public types of a package accessible to other modules. A package that is not exported is hidden, even if its classes are public.
module com.example.data {
requires transitive java.sql;
exports com.example.data;
}Here com.example.data is the API. A package such as com.example.data.internal stays private to the module. Code in other modules that tries to import it fails to compile. Reflection fails too, with an IllegalAccessException, unless the module opens the package.
An application module that nobody else depends on, like com.example.app above, often has no exports at all. That is fine. The java launcher can still run its main class, because the launcher is not a module that needs access to the package.
Two related directives are worth knowing about, though you do not need them to use jlink:
exports ... tolimits the export to named modules.opensallows deep reflection into a package at run time. Frameworks that use reflection on your classes often require it.
How the descriptor feeds jlink
jlink starts from the root modules you give it with --add-modules. It then resolves the module graph, which means it reads each root's descriptor, adds every module named in a requires directive, and repeats for each of those. The resulting set is what goes into the image.
The diagram is easier to read with the concrete example, so here it is as a proper block:
The image for this application contains com.example.app, java.logging, and java.base. Nothing else from the JDK is included, which is why the image is small.
Compiling the module
Place module-info.java at the top of the source directory, next to the com directory. Then compile with the commands from the tutorial's example:
mkdir -p out/com.example.app mods
javac -d out/com.example.app \
src/module-info.java src/com/example/app/Main.java
jar --create --file mods/app.jar \
--main-class com.example.app.Main \
-C out/com.example.app .
jdeps --print-module-deps mods/app.jarWhen javac sees a module-info.java among its inputs, it compiles in module mode. The output directory then contains module-info.class at its root. The jar command packages that into a modular JAR. The last command asks jdeps which JDK modules the JAR uses. For this application it prints java.logging. The next chapters return to jdeps.
Common errors
package java.util.logging is not visible: the descriptor lacksrequires java.logging;. The error message even suggests the missing directive.module not found: com.example.app: the module is not on the module path, or the JAR lacksmodule-info.class. Check that you putmodule-info.javain the compile command and thatjarpackages the output directory.- A
module-info.javathat compiles but the runtime fails withNoClassDefFoundError: a dependency that is onlyrequires static, or one loaded by reflection and never named in any descriptor. - Descriptor in the wrong place:
module-info.javamust be at the root of the source tree, not inside a package directory.
Reflection and Class.forName are the biggest source of surprises. The module system and jdeps see only static dependencies. A module that your code loads by name as a string is invisible to both, so you must add it with --add-modules yourself.
Exercise
A module declares requires static java.compiler; and nothing else requires java.compiler. You build an image with jlink --add-modules com.example.tools. Will java.compiler be in the image, and what can go wrong?
Show answer
No. jlink does not follow requires static edges, so java.compiler is left out unless you add it with --add-modules java.compiler. If the code uses a class from that module at run time without guarding for its absence, it fails with NoClassDefFoundError in the image.
Exercise
Your module com.example.data has a public method returning java.sql.Connection. Should the descriptor use requires java.sql; or requires transitive java.sql;, and why?
Show answer
Use requires transitive java.sql;. The type appears in the module's public API, so every consumer needs to read java.sql too. With transitive, they get it automatically instead of each adding its own requires.