Sub topic 8
The modular JDK and java.base
This chapter assumes you can read a module-info.java, know what requires and exports do, and can compile and run on the module path. Here you will see how the JDK itself is built from modules, why every module depends on java.base, and how to find which JDK modules your code needs.
The JDK is a set of modules
Before Java 9, the platform classes shipped in one large archive, rt.jar. Since Java 9, the JDK is divided into about seventy modules. Each one has a name, a descriptor, and a list of exported packages, like the modules you write. Your code depends on them with the same requires directive.
The module names follow a convention:
- Modules starting with
java.are part of the Java SE specification. Portable code can rely on them. - Modules starting with
jdk.are specific to the JDK. They hold tools, implementation details, and extra APIs such asjdk.httpserver.
| Module | What it provides |
|---|---|
| `java.base` | Core types: `java.lang`, `java.util`, `java.io`, `java.nio`, `java.net`, `java.time`, `java.security` |
| `java.logging` | `java.util.logging` |
| `java.sql` | JDBC (`java.sql`, `javax.sql`) |
| `java.xml` | XML parsing and transformation |
| `java.net.http` | The `HttpClient` API (Java 11 and later) |
| `java.desktop` | AWT, Swing, Java 2D, image I/O |
| `java.management` | JMX |
| `jdk.unsupported` | `sun.misc.Unsafe` and a few other legacy internal APIs |
The java.se module is an aggregator. It has no code of its own and re-exports the other Java SE modules through requires transitive. Writing requires java.se; works, but it pulls in everything, which defeats the purpose of a small module graph. Prefer naming the modules you use.
java.base is always there
java.base contains the types nothing can run without, including Object, String, and the collections framework. It is the only module that requires no other module, and every other module reads it. The compiler adds requires java.base; to every descriptor automatically, so you never write it.
That is why module com.example.greeter { exports ...; } compiles even though the code uses String. The dependency on java.base exists in the compiled module-info.class anyway.
Everything else in the JDK must be declared. Consider this module:
// src/com.example.logdemo/module-info.java
module com.example.logdemo {
requires java.logging;
}// src/com.example.logdemo/com/example/logdemo/Main.java
package com.example.logdemo;
import java.util.logging.Logger;
public class Main {
public static void main(String[] args) {
Logger log = Logger.getLogger(Main.class.getName());
log.info("Started");
}
}javac -d out --module-source-path src -m com.example.logdemo
java --module-path out -m com.example.logdemo/com.example.logdemo.MainIf you delete the requires java.logging; line, compilation fails with a message like this:
error: package java.util.logging is not visible
(package java.util.logging is declared in module java.logging, but module com.example.logdemo does not read it)The message names the package, the module that owns it, and the missing readability edge. Fix it by adding the requires directive.
Inspecting the JDK's modules
The java launcher can describe the modules in the runtime you are using:
java --list-modules
java --describe-module java.sql
java --describe-module java.base--list-modules prints one module per line with its version, for example java.logging@12. --describe-module prints the module's exports, its requires directives, and the services it uses or provides. For java.sql, you will see lines such as requires transitive java.logging and requires transitive java.xml. This is why a module that requires java.sql can also use java.util.logging types: java.sql exposes them in its API through implied readability, which a later chapter covers in detail.
The next diagram shows part of the JDK's module graph.
What the JDK hides
Modules also protect the JDK's own internals. java.base contains many packages that it does not export, such as jdk.internal.misc and sun.security.*. Code outside the module cannot compile against them, and since JDK 16 reflection cannot reach them either unless you pass --add-opens. The jdk.unsupported module is the exception. It exports a few widely used internal APIs, such as sun.misc.Unsafe, so older libraries keep working.
You can ask jdeps which of your dependencies use JDK internals:
jdeps --jdk-internals app.jarThe report suggests supported replacements where they exist.
Modules that were removed
The Java EE and CORBA modules shipped with JDK 9 and 10 but were removed in JDK 11. They included java.xml.bind (JAXB), java.activation, java.xml.ws, and java.corba. Code that imports javax.xml.bind or javax.annotation.PostConstruct compiles on JDK 8 but fails on JDK 11 and later with a "package does not exist" error. Add the matching library from Maven Central instead. Those libraries are ordinary jars, so you place them on the module path or classpath like any other dependency.
Finding the modules you need
Guessing which java.* modules your application uses is error-prone, so let jdeps do it:
jdeps --print-module-deps app.jarThis prints a comma-separated list such as java.base,java.logging,java.sql. It analyzes the compiled bytecode, so it misses modules that you reach only through reflection or ServiceLoader. Add those by hand. The list is also the input for jlink, which builds a runtime image containing only those modules.
On the classpath, the unnamed module has no descriptor to declare anything. In that case the runtime resolves a default set of root modules: java.se plus every jdk.* module that exports an API. This is why classpath programs written for Java 8 keep working without any requires.
Common mistakes
- Compiling code that uses
java.util.loggingorjava.sqlwithout the matchingrequiresdirective. - Writing
requires java.se;out of convenience, which makes the runtime image far larger than necessary. - Using
sun.misc.Unsafeand forgettingrequires jdk.unsupported;. - Migrating to JDK 11 or later and finding that the JAXB or
javax.annotationpackages are gone.
Exercise
Your module uses java.sql.Connection and java.util.logging.Logger. Its descriptor contains only requires java.sql;. Does it compile, and why?
Show answer
Yes. java.sql declares requires transitive java.logging, so any module that requires java.sql also reads java.logging. You can confirm this with java --describe-module java.sql. Adding an explicit requires java.logging; is still good practice if your own code uses it directly.
Exercise
Why can you write a module without ever declaring requires java.base;, and which command lists the modules an application jar needs?
Show answer
The compiler adds a mandated requires java.base; to every module descriptor, because java.base holds types such as Object and String that nothing can run without. jdeps --print-module-deps app.jar lists the JDK modules the jar depends on.