Sub topic 4
The modules inside the JDK
This chapter assumes you can compile and run Java with javac and java, and that you know a module declares its dependencies in module-info.java. The previous chapters covered the module system and requires. Here you will learn what the JDK itself is made of, because jlink can only keep or drop whole JDK modules.
The JDK is a set of modules
Since Java 9, the JDK is not one large library. It is a collection of named modules, each with its own module-info. Each module owns a set of packages and declares which other modules it needs. When you build an image with jlink, you choose a set of root modules. jlink adds those modules and everything they require, directly or indirectly, and leaves the rest out.
Knowing which module owns which package tells you why an image works or fails. A class that lives in a module you left out cannot be loaded at run time.
Three families of modules
JDK modules fall into three groups by name and purpose.
java.baseis the foundation. Every module requires it implicitly, so it is in every image. It holdsjava.lang,java.util,java.io,java.nio,java.net,java.security,java.time, and more.java.*modules are the standard Java SE API, such asjava.sqlandjava.logging. Any compliant Java SE implementation provides them.jdk.*modules are specific to the JDK. Some hold tools likejdk.compiler(javac) andjdk.jartool(jar). Others hold extra APIs and implementation pieces, such asjdk.httpserverandjdk.unsupported.
The exact number of modules depends on the release and the vendor. Recent JDKs have roughly 70.
Modules you will meet most often
| Module | What it contains | Typical reason to need it |
|---|---|---|
| `java.base` | Core language and utility packages | Always present |
| `java.logging` | `java.util.logging` | Applications and libraries that use JUL |
| `java.sql` | JDBC (`java.sql`, `javax.sql`) | Database access |
| `java.xml` | XML parsing and transformation (JAXP) | Config files, SOAP, many libraries |
| `java.desktop` | AWT, Swing, `java.beans`, image I/O | GUIs, and any code that uses `java.beans` |
| `java.net.http` | `HttpClient` | HTTP calls |
| `java.management` | JMX | Monitoring and metrics libraries |
| `java.naming` | JNDI | Application servers, LDAP |
| `jdk.httpserver` | `com.sun.net.httpserver` | Embedded HTTP servers |
| `jdk.unsupported` | `sun.misc.Unsafe` and related classes | Older libraries that depend on `Unsafe` |
| `jdk.localedata` | Locale data beyond English | Formatting for non-US locales |
| `jdk.zipfs` | `FileSystem` provider for ZIP files | Treating a JAR or ZIP as a file system |
Inspecting the JDK's modules
You do not need to memorize this list. The java launcher can tell you what your JDK contains.
java --list-modules
java --list-modules | grep '^jdk\.' | head
java --describe-module java.sql--list-modules prints every module in the runtime, with its version. --describe-module shows one module's descriptor. For java.sql on a recent JDK, the output looks like this (abridged):
java.sql@21
exports java.sql
exports javax.sql
requires java.base mandated
requires java.logging transitive
requires java.transaction.xa transitive
requires java.xml transitive
uses java.sql.DriverThree things in this output matter for jlink:
exportslists the packages your code may use. Packages that are not exported, such assun.*andjdk.internal.*, are strongly encapsulated by default since JDK 16. That is true in an image as well.requires ... transitivemeans any module that requiresjava.sqlalso getsjava.logging,java.xml, andjava.transaction.xareadable.jlinkincludes them either way.usesnames a service interface. Modules that provide that service are not pulled in automatically. A later chapter covers this under--bind-services.
How dependencies pull modules in
Adding one module to an image adds its whole dependency closure. This diagram shows the closure for java.sql.
So --add-modules java.sql gives you five modules in total. That is still far smaller than the full JDK.
Checking the modules at run time
You can ask the running JVM which modules it has. This is useful for checking an image later, because the same program reports different results on a full JDK and on a trimmed image.
public class ListModules {
public static void main(String[] args) {
ModuleLayer.boot().modules().stream()
.map(Module::getName)
.sorted()
.forEach(System.out::println);
boolean hasSql = ModuleLayer.boot().findModule("java.sql").isPresent();
System.out.println("java.sql present: " + hasSql);
}
}Run it on your JDK with java ListModules.java. The source launcher compiles and runs the file in one step. On a full JDK you will see dozens of modules. In an image that contains only java.base, you will see one line plus java.sql present: false.
Aggregator modules
Some modules exist only to group others. The most important is java.se, which requires transitively every module in the Java SE API. Passing --add-modules java.se gives you the full standard API, which is a convenient starting point. It is also much larger than most applications need. Use it to get a program running, then narrow the list with jdeps (covered in a later chapter).
Common errors
Most problems with JDK modules come from leaving one out. They show up in two ways.
At link time. If you name a module that does not exist, jlink stops with a message such as Error: Module java.sqll not found. Check the spelling against java --list-modules. The same error appears if the --module-path does not include the JDK's jmods directory, which the next chapter covers.
At run time. Image creation can succeed even though the application later fails. Code that reaches a package from a missing module throws NoClassDefFoundError or ClassNotFoundException. The usual causes are:
- Reflection and
Class.forName.jdepsreads bytecode references. It cannot see a class name held in a string, so it will not report that module. - Hidden JDK dependencies. A library calls
java.beansorjava.sqlinternally. Your own code never imports those packages, but the library needsjava.desktoporjava.sql. sun.misc.Unsafe. Many older libraries use it, which requiresjdk.unsupported.- Locales. A program that formats dates or numbers for non-English locales falls back to English if
jdk.localedatais missing. No exception is thrown, so this one is easy to miss. - TLS and elliptic curves. Before JDK 22,
jdk.crypto.ecwas a separate module, and its absence made some HTTPS connections fail with handshake errors. From JDK 22 the code lives injava.base.
Where this leads
You now have the vocabulary: java.base in every image, java.* for the standard API, jdk.* for JDK-specific parts, and transitive requirements that widen the image. The next chapter shows where jlink finds these modules on disk, in the jmods directory.
Exercise
Your application declares requires java.sql; and nothing else. Which JDK modules end up in a jlink image built with --add-modules <your module>? Name them, and say why java.logging is among them even though you never required it.
Show answer
java.base, java.sql, java.logging, java.xml, and java.transaction.xa. java.sql declares requires transitive on java.logging, java.xml, and java.transaction.xa, and jlink includes the full dependency closure of the root modules. java.base is required by every module.
Exercise
A trimmed image runs your program, but dates print in English where your test machine printed German. No exception appears. Which module is the likely cause, and how can you confirm it from inside the program?
Show answer
jdk.localedata is probably missing from the image. Confirm with ModuleLayer.boot().findModule("jdk.localedata").isPresent(), which returns false, or with build/runtime/bin/java --list-modules on the image. Fix it by adding --add-modules jdk.localedata when you run jlink.