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 application needs.
What module-info.java does
A module is a named group of packages. Its descriptor is a file called module-info.java, placed at the root of the module's source tree. The compiler turns it into module-info.class, which sits at the root of the module's JAR.
The descriptor states two things:
- Which other modules this module needs (
requires). - Which of its own packages other modules may use (
exports).
jlink reads the requires clauses to decide which JDK modules go into the image. If a module is not reachable through requires, it is left out.
A minimal descriptor
module com.example.app {
requires java.logging;
}The module name, com.example.app, follows the same reverse-domain convention as packages. The requires java.logging; line says that this module uses the java.util.logging API, which lives in the JDK module java.logging.
You never write requires java.base;. Every module reads java.base automatically, because Object, String and the other core classes live there.
The code that matches it
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");
}
}The import of java.util.logging.Logger is what creates the real dependency. Remove the requires line and the compiler reports that the package is not visible.
How the dependency graph forms
When you run jlink with --add-modules com.example.app, it starts at that root and follows every requires edge. This walk is called resolution. The result here is three modules: your application, java.logging and java.base.
requires and exports
| Directive | Meaning | Example |
|---|---|---|
| `requires` | This module needs another module at compile time and run time. | `requires java.sql;` |
| `requires transitive` | Modules that read this module also read the named one. | `requires transitive java.sql;` |
| `exports` | Makes a package's public types available to other modules. | `exports com.example.api;` |
The example application exports nothing. That is fine, because nothing else depends on it. A library module would add exports for the packages it wants others to call.
Compiling and packaging
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.jarThe javac command compiles the descriptor together with the source. The jar command packages the output as a modular JAR and records the main class. The last command, jdeps, prints the JDK modules the JAR needs. For this application it should print java.base,java.logging. The next chapters use jdeps in more detail.
Common errors
package java.util.logging is not visible: therequires java.logging;line is missing.module not found: java.sql: you misspelled a module name, or the module is not on the module path.- A module works with
javacbut is missing from the image: you forgot to list it inrequires, and the code reaches it only through reflection. Resolution cannot see reflective use, so add therequiresline yourself.
Exercise
Your application imports java.sql.Connection but its module-info.java contains only requires java.logging;. What happens when you compile, and what is the fix?
Show answer
Compilation fails with an error that the package java.sql is not visible, because the module does not read java.sql. Add requires java.sql; to the descriptor. jlink will then include java.sql in the image.
Exercise
Why does the descriptor in this chapter not list requires java.base;?
Show answer
Every module reads java.base automatically, so the declaration is implicit. jlink always includes java.base in an image.