Java modules, reflection, and smaller jlink runtimes
Use JPMS to define module readability and reflection access, then inspect Spring Boot dependencies and build a focused Java 21 runtime with jlink.
A class can be present at runtime and still be inaccessible across a Java module boundary. I use the module graph to explain that failure, then use the same dependency model to trim a Java runtime with
jlink.
The examples use Java 21 LTS, Spring Boot 3.x, and a multi-stage Docker build.
What JPMS adds to the classpath model
The module system introduced in Java 9 makes dependency and access relationships explicit. A named module declares other modules it reads. It also controls which packages it exports and which packages it opens for deep reflection. A package can be visible to ordinary Java calls while still blocking reflective access to non-public members.
| Directive | Purpose |
|---|---|
requires | Makes another module readable |
requires transitive | Lets consumers read a dependency exposed by this module |
exports | Exposes accessible public types, optionally to named consumers |
opens | Allows deep reflection, optionally for named consumers |
uses and provides ... with | Connects service consumers and providers through ServiceLoader |
The payment module declares dependencies and exports its public API. The order module can open a package to a framework that needs reflective access to its entity or injection fields.
1
2
3
4
5
6
7
// payment-module/src/main/java/module-info.java
module com.example.payment {
requires transitive java.sql; // If the public API exposes a java.sql type
exports com.example.payment.api;
provides com.example.payment.spi.PgClient
with com.example.payment.internal.TossPgClient;
}
1
2
3
4
5
6
7
8
9
10
// order-module/src/main/java/module-info.java
module com.example.order {
requires com.example.payment;
requires spring.context;
exports com.example.order.service;
// Allow Spring and Hibernate to make reflective access.
opens com.example.order.domain to spring.core, org.hibernate.orm.core;
uses com.example.payment.spi.PgClient;
}
flowchart TD
ORDER["Order module"] -->|"requires"| PAYMENT["Payment module"]
PAYMENT -->|"exports"| API["Payment API"]
PAYMENT -->|"requires transitive"| SQL["java.sql"]
ORDER -->|"opens an entity package"| RUNTIME["Persistence framework"]
Why a module can reject a package or reflection
If two named modules export the same package to a reader, the module system cannot choose between the definitions. That conflict can appear during a classpath-to-module-path migration when several older artifacts carry the same package. Find both owners and remove the overlap before trying to change access flags.
An InaccessibleObjectException usually points to a different boundary. The class may be readable while its package is not open to the code attempting deep reflection. Open the specific package to the framework module where possible. A deployment-wide --add-opens flag is useful when isolating the cause. Record any flag the application has to keep as an explicit compatibility requirement.
flowchart TD
A["Resolve named modules"] --> B{"Duplicate exported package?"}
B -->|"Yes"| C["Resolve the package conflict"]
B -->|"No"| D["Check access rules"]
D -->|"Deep reflection"| E["Open the target package"]
Building a focused runtime from JDK modules
In the Docker example, a build stage extracts the Spring Boot archive, inspects application classes and dependencies with jdeps, and passes the required JDK modules to jlink. The final stage contains the resulting runtime and application. The application can still run with classpath-style loading. A modular runtime and a modular application are separate choices.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Dockerfile (Java 21 and Spring Boot 3.x multi-stage jlink build)
FROM eclipse-temurin:21-jdk-jammy AS builder
WORKDIR /workspace
COPY build/libs/app.jar app.jar
# 1. Extract the Spring Boot archive
RUN mkdir -p unpacked && (cd unpacked && jar -xf ../app.jar)
# 2. Inspect JDK module dependencies with jdeps
RUN jdeps --ignore-missing-deps -q \
--recursive \
--multi-release 21 \
--print-module-deps \
--class-path 'unpacked/BOOT-INF/lib/*' \
app.jar > deps.info
# 3. Create a custom runtime, including jdk.crypto.ec for TLS
RUN jlink \
--add-modules "$(cat deps.info),jdk.crypto.ec" \
--strip-debug \
--no-man-pages \
--no-header-files \
--compress=zip-6 \
--output /custom-jre
# 4. Copy the runtime and app into the slim base image
FROM debian:bookworm-slim
ENV JAVA_HOME=/opt/java/openjdk
ENV PATH="${JAVA_HOME}/bin:${PATH}"
COPY --from=builder /custom-jre "${JAVA_HOME}"
COPY --from=builder /workspace/app.jar /app/app.jar
WORKDIR /app
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/app/app.jar"]
The source measurements give an approximate size comparison for the illustrated images. Verify the actual tags and architecture before using it for a release budget.
| Image setup | Approximate size |
|---|---|
| Full Java 21 JDK base | 430 MB |
| Java 21 JRE base | 270 MB |
| Debian slim plus a custom runtime | 120–140 MB |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
{
"type": "bar",
"data": {
"labels": [
"eclipse-temurin:21-jdk-jammy",
"eclipse-temurin:21-jre-jammy",
"debian:bookworm-slim + jlink JRE"
],
"datasets": [
{
"label": "Runtime image size (MB)",
"data": [430, 270, 130]
}
]
},
"options": {
"indexAxis": "y",
"plugins": {
"title": {
"display": true,
"text": "Java 21 container images by approximate size"
},
"legend": {
"display": false
}
},
"scales": {
"x": {
"beginAtZero": true,
"title": {
"display": true,
"text": "Approximate size (MB)"
}
}
}
}
}
A smaller runtime contains fewer standard modules. Static dependency analysis can miss modules needed through reflection, providers, or configuration. The example explicitly includes jdk.crypto.ec for deployments that need elliptic-curve TLS support.
Checking the result in the deployment environment
Keep named dependency artifacts stable. Automatic module names derived from filenames can shift after a packaging change, so prefer libraries with explicit module names for a modular build. Keep exports constrained to supported public APIs, and open individual packages only where runtime reflection needs them.
Validate the final container with the actual startup command and production-like configuration. Exercise HTTPS with the intended trust material, JDBC driver discovery, required character encodings, and agents. Check java --list-modules inside the built image while diagnosing failures. Compare a base-image upgrade against the same tests and record which extra modules were required due to dynamic use.