Sub topic 4
The modules inside the JDK
This chapter assumes you can compile and run Java programs, know what a JAR and the classpath are, and have read the previous chapters on module descriptors. Here you will learn how the JDK itself is split into modules and how that split decides what jlink can leave out.
From one big runtime to many modules
Before Java 9, the runtime was a single archive called rt.jar. Any program that needed one class from it got all of it. Since Java 9, the JDK is divided into named modules. Each module has a descriptor that states what it exports and what it requires. jlink reads those descriptors to work out which modules your image needs.
You can see the modules in your own JDK with this command:
java --list-modulesEach line shows a module name and a version, such as java.base@21. A full JDK lists roughly seventy modules. A JRE built by jlink lists only the ones you asked for, plus what they require.
The modules you meet most often
JDK modules follow a naming convention. Names starting with java. are part of the Java SE standard. Names starting with jdk. are specific to the JDK implementation.
| Module | What it provides |
|---|---|
| `java.base` | Core classes: `java.lang`, `java.util`, `java.io`, `java.nio`, `java.net` |
| `java.logging` | `java.util.logging` |
| `java.sql` | JDBC API |
| `java.xml` | XML parsing and processing |
| `java.net.http` | The HTTP client |
| `java.desktop` | AWT, Swing, and image APIs |
| `jdk.compiler` | The `javac` compiler |
| `jdk.httpserver` | The built-in simple HTTP server |
java.base is special. Every module requires it implicitly, so it is always in an image. A program that only uses core classes needs nothing else, which makes for a very small runtime.
Dependencies between JDK modules
JDK modules depend on each other. For example, java.sql requires java.logging, java.xml, and java.transaction.xa. When you add java.sql to an image, jlink pulls in those three modules too. This is called the transitive closure of the dependencies.
You can inspect a module's descriptor with java --describe-module:
java --describe-module java.sqlThe output lists exports (packages other modules may use) and requires (modules it depends on). A line beginning requires transitive means that any module requiring java.sql also gets that dependency automatically.
The diagram below shows part of this graph.
Why this matters for jlink
The module boundaries set the smallest unit you can remove. You cannot drop half of java.xml. You either include the module or you do not. So the size of your image depends on which modules your code needs and how many each of them pulls in.
A few practical points follow from this:
- Requiring
java.desktopbrings in a large amount of code, so avoid it unless you need it. - Modules such as
jdk.compilerandjdk.jshellare tools. A server application normally does not need them. - A missing module shows up at run time as a
NoClassDefFoundErrororClassNotFoundException, not at link time, when your code uses reflection or loads classes by name.
Exercise
You add java.sql to a jlink image with --add-modules java.sql. Your code never uses java.util.logging directly. Will java.logging be in the image, and why?
Show answer
Yes. java.sql requires java.logging, and jlink includes every module in the transitive closure of the modules you add. You can check with java --describe-module java.sql and, after linking, with bin/java --list-modules.