java.lang.reflect.InaccessibleObjectException: Unable to make field private final byte[] java.lang.String.value accessible: module java.base does not "opens java.lang" to unnamed module. Or a colleague says “just add the jar to the module path” and the compiler answers package com.acme.greeter.internal is not visible. Both errors come from the same place: the Java Platform Module System, JPMS, which has been part of Java since version 9 and which most developers meet only as an error message.
This post starts from those error messages and works backwards to the idea behind them. You will write five small modules, break them on purpose in the ways people actually break them, read the exact error text each time, and then use two JDK tools, jdeps and jlink, that make modules worth the trouble. Everything was compiled and run on JDK 25; the companion code is asmhatre/javademos/jpms-basics, and every console block below is quoted from a file in its output/ folder, produced by one script, run.sh.
You do not need to have used modules before. You do need to know what a package, a public class and a JAR file are. The sections get more technical as you go, and the collapsible boxes (“Going deeper”) hold the reference-level detail so that the main text stays one continuous read.
Versions. JDK 25 (Eclipse Temurin 25.0.5.1+2, an LTS release; JDK 25 reached GA on 2025-09-16 per the OpenJDK JDK 25 page). The module system itself arrived in JDK 9 as JEP 261; strong encapsulation of the JDK’s own internals became the default in JDK 16 (JEP 396) and the--illegal-accessescape hatch stopped working in JDK 17 (JEP 403). Plainjavac,java,jar,jdepsandjlink; no Maven or Gradle.jlinkwas fed the matching Temurinjmodsdownload, because the main Temurin 25 tarball does not contain ajmodsfolder (section on jlink).
1. On the class path, “public” means public to everyone, and that is the problem
Before Java 9 a Java program was a pile of classes found on the class path. Every JAR was dumped into one flat namespace. If a class waspublic, any other class on the class path could call it, and with reflection could also read its private fields. A library author who wanted a helper class to be “internal” had only a naming convention (...internal... in the package name) and a note in the docs. Users ignored both. The JDK had the same problem with its own classes: code all over the ecosystem reached into sun.misc.Unsafe and into private fields of java.lang classes, so the JDK could not change them without breaking programs.
Secrets sits next to everything else and anyone can call it. The right box is what a module adds: a named group of packages with a file, module-info.java, that lists which packages other modules may use. The compiler and the JVM both refuse access across the wall. JEP 261 says the compiler, the VM and the run-time libraries implement modules as “a fundamental new kind of Java program component” and provide “reliable configuration and strong encapsulation of modules in all phases of development”, and it adds a link-time phase between compile time and run time, with the jlink tool. Those are the three things this post covers: encapsulation (exports, opens), configuration (requires, split packages, jdeps) and linking (jlink).
Going deeper: what JPMS does not do
JPMS does not manage versions. requires names a module, not a module at a version, so if you need several versions of a library at once you are still in class-loader territory. I did not test what the JVM does with two versions of one module on a module path. The one place a version is recorded is the module descriptor (--module-version on jar), and it is informational.
The module system also never replaced the class path. A JAR on the class path ends up in the unnamed module, which reads every module but cannot be named in a requires by a named module; section “On the class path none of this applies” shows the consequences with real output. That is why you can adopt modules one library at a time.
- How the JDK itself is carved up: JDK vs JRE vs JVM
- Class loaders, which modules sit on top of rather than replace: Java ClassLoaders Explained
- JEP 261: Module System
2. A module is a package group plus a module-info.java that says what it needs and what it shows
The smallest useful module system needs two modules: a library that hides something and an application that uses it. The companion project has five, because five is what it takes to demonstrate everything below, but start with these two.com.acme.greeter is the library. Its descriptor lives in module-info.java at the root of the module’s source folder:
module com.acme.greeter {
// Greeting is part of Greeter's public signature, so every reader of greeter must also read model.
requires transitive com.acme.model;
// The public surface. Anything not listed here is invisible outside the module,
// even if the class is declared `public`.
exports com.acme.greeter.api;
// com.acme.greeter.internal is deliberately NOT exported.
// We look up implementations of Formatter at run time instead of naming them.
uses com.acme.greeter.api.Formatter;
}
That file is module-info.java. It says four things. requires transitive com.acme.model means greeter needs the model module and anybody who reads greeter gets model for free (section 3 shows why that matters). exports com.acme.greeter.api makes one package visible. The package com.acme.greeter.internal is simply not mentioned, so it is invisible outside the module even though its class is public. uses declares a service, which section 7 explains.
The application’s descriptor is shorter:
module com.acme.app {
requires com.acme.greeter; // model comes along because greeter says `requires transitive`
requires com.acme.mapper;
// Deep reflection into the domain package is allowed for exactly one module.
opens com.acme.app.domain to com.acme.mapper;
}
That is module-info.java. Ignore the opens line for now; it is the subject of section 4. Compiling and running both uses two new options: the module source path for javac and the module path for java.
$ javac -d build/mods --module-source-path src --module com.acme.app,com.acme.formal
$ java --module-path build/mods --module com.acme.app/com.acme.app.Main hello
Greeting[text=Hello, Mumbai! (s3cr3t), formatter=built-in]
These are from 01-build-and-run.txt. --module-source-path src tells javac that src/ contains one folder per module, named after the module. I asked for only com.acme.app and com.acme.formal; javac found and compiled the other three itself because they are required. --module-path (short form -p) is the module-aware cousin of -cp, and -m module/class says which class to start. The program, Main.java, is ordinary Java; nothing in it mentions modules, because the module rules live in the descriptors.
requires; the thick green one is requires transitive, which is why the application can use Greeting from model without ever writing requires com.acme.model. Every module reads java.base without asking, which is the module that holds java.lang, java.util and the rest of the core. You can ask the JVM what it knows, and it prints the same facts in a fixed format:
$ java -p build/mods --describe-module com.acme.greeter
com.acme.greeter file:///.../jpms-basics/build/mods/com.acme.greeter/
exports com.acme.greeter.api
requires com.acme.model transitive
requires java.base mandated
uses com.acme.greeter.api.Formatter
contains com.acme.greeter.internal
$ java -p build/mods --describe-module com.acme.app
com.acme.app file:///.../jpms-basics/build/mods/com.acme.app/
requires java.base mandated
requires com.acme.greeter
requires com.acme.mapper
qualified opens com.acme.app.domain to com.acme.mapper
contains com.acme.app
From 02-describe.txt. Two details are worth noticing. requires java.base mandated: you never wrote it, the compiler added it. And qualified opens com.acme.app.domain to com.acme.mapper: “qualified” means to specific modules only, which is the safe form of opens.
Going deeper: every directive in a module-info.java
A descriptor has a small vocabulary. All rows except the last were exercised in the companion project, with one exception: I did not run the qualified form exports p to m. The last row is described from the language specification, not from a run.
| Directive | Meaning | Used in this post |
|---|---|---|
requires m; | m must be present and is readable by this module | app, formal |
requires transitive m; | as above, and modules that read me also read m | greeter requires transitive model |
exports p; / exports p to m1, m2; | public types of package p are accessible at compile time and run time, to everyone or to the named modules | greeter, model, mapper |
opens p; / opens p to m; | at run time all members of p, private ones included, are accessible by reflection | app opens domain to mapper |
uses S; / provides S with C; | service consumer and provider for ServiceLoader | greeter uses, formal provides |
requires static m; and open module m { } | m is needed to compile but optional at run time; every package of the module is opened (no individual opens allowed) | not exercised |
Package names in a descriptor must exist: exports com.acme.nothing; for a package with no classes is a compile error (03-compile-errors.txt, last block).
Module names follow package-name style and the compiler warns, with [module] lint, about names ending in digits. That is why the broken demo modules are called badinternal and not bad1.
- The whole layout: src/ (one folder per module) and broken/ (sources that must fail)
- java.lang.module package summary (Java SE 25)
3. Compile time: the three errors you will see first
Most of what you will meet when you first move a project onto the module path is the compiler saying not visible or not accessible, and the words in parentheses tell you which of three different mistakes you made. Each block below is a tiny module inbroken/ that is meant to fail.
The package is not exported
package com.acme.badinternal;
import com.acme.greeter.internal.Secrets; // public class, but the package is not exported
That import comes from Main.java; the class it names is declared public.
$ javac -d build/b1 -p build/mods $(find broken/01-internal-access -name "*.java")
broken/01-internal-access/com/acme/badinternal/Main.java:3: error: package com.acme.greeter.internal is not visible
import com.acme.greeter.internal.Secrets; // public class, but the package is not exported
^
(package com.acme.greeter.internal is declared in module com.acme.greeter, which does not export it)
1 error
[exit code 1]
From 03-compile-errors.txt. The fix is on the owner’s side (exports the package) or, better, on yours (use the public api package instead). The message does not say “private”; it says declared in module com.acme.greeter, which does not export it.
You forgot to say you need the module
$ javac -d build/b2 -p build/mods $(find broken/02-no-requires -name "*.java")
broken/02-no-requires/com/acme/badnoreq/Main.java:3: error: package com.acme.greeter.api is not visible
import com.acme.greeter.api.Greeter;
^
(package com.acme.greeter.api is declared in module com.acme.greeter, but module com.acme.badnoreq does not read it)
1 error
[exit code 1]
Same “not visible”, different parenthesis: but module com.acme.badnoreq does not read it. The package is exported, but the module that wants it never wrote requires com.acme.greeter. A module can only use what it has declared a dependency on, even if the jar is sitting right there on the module path.
You used a type you were never given
This one is more subtle and it is the reasonrequires transitive exists. Greeter.greet returns a Greeting, and Greeting lives in a third module, model. I built a copy of greeter whose descriptor says plain requires com.acme.model instead (the diff is in the transcript) and compiled an application against it that does nothing but call greet(...).text():
$ javac -d build/b3 -p build/mods-nt $(find broken/03-not-transitive -name "*.java")
broken/03-not-transitive/com/acme/badtransitive/Main.java:8: error: Greeting.text() in package com.acme.model is not accessible
System.out.println(g.text());
^
(package com.acme.model is declared in module com.acme.model, but module com.acme.badtransitive does not read it)
1 error
[exit code 1]
The application never named com.acme.model in source code, yet the compiler complains, because the method text() belongs to a class in a module this application does not read. With requires transitive in greeter (the real descriptor) the same code compiles. The rule of thumb: if a type from module X appears in the public signature of module Y, then Y should say requires transitive X. It is the first thing library authors get wrong.
Read the parenthesis. “package X is not visible” has three possible follow-ups, all captured above: which does not export it (add or use an exported package), but module M does not read it (addrequires) and, for method calls, is not accessible with the same parenthesis (addrequires transitivein the library). The headline is identical; only the parenthesis tells you where to fix it.
Going deeper: cycles, and what the compiler checks
Modules cannot depend on each other in a circle. Two modules that require one another fail at compile time and the compiler reports the cycle once from each side:
$ javac -d build/b4 --module-source-path broken/04-cycle --module cyc.a,cyc.b
broken/04-cycle/cyc.b/module-info.java:1: error: cyclic dependence involving cyc.a
module cyc.b { requires cyc.a; exports cyc.b; }
^
broken/04-cycle/cyc.a/module-info.java:1: error: cyclic dependence involving cyc.b
module cyc.a { requires cyc.b; exports cyc.a; }
^
2 errors
[exit code 1]
This is from 03-compile-errors.txt and the two sources are under broken/04-cycle/. If you inherit a codebase that has such a cycle, the usual fix is to extract the shared types into a third module that both require, which is the same refactoring you would want without modules.
- The exact error text above is javac 25.0.5.1; the wording of these messages has changed between releases, so match on the parenthesised part, not the headline
- javac reference (module options: –module-source-path, –module-path, –add-exports)
4. Reflection is a separate door: exports is not opens
Everything so far was the compiler. The second half of module encapsulation happens at run time, and it catches the code that the compiler never sees: reflection. Frameworks that turn JSON into objects, inject fields or build proxies all callsetAccessible(true) on members they were never meant to touch, and that call is now policed per module.
The rule has two parts. exports p lets other modules use the public types of package p, at compile time and through reflection. opens p additionally lets them reach every member of p, private fields included, through reflection. A package can be exported without being opened (the usual state), opened without being exported (frameworks’ favourite), both, or neither.
setAccessible(true). Only with opens does the private field become reachable. The red cells are not predictions: each one is an error captured from a run, shown next. The Main class in Main.java has one small method per cell.
First the public method, which needs nothing more than exports:
$ java -p build/mods -m com.acme.app/com.acme.app.Main reflect-public
invoked public method reflectively: Greeting[text=Hello, reflection! (s3cr3t), formatter=built-in]
Now the private field of the same class, in the same exported package:
static void reflectPrivate() throws Exception {
Field f = Greeter.class.getDeclaredField("prefix");
System.out.println("found the field: " + f);
f.setAccessible(true);
System.out.println("read it: " + f.get(new Greeter()));
}
$ java -p build/mods -m com.acme.app/com.acme.app.Main reflect-private
found the field: private final java.lang.String com.acme.greeter.api.Greeter.prefix
Exception in thread "main" java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.lang.String com.acme.greeter.api.Greeter.prefix accessible: module com.acme.greeter does not "opens com.acme.greeter.api" to module com.acme.app
at com.acme.app/com.acme.app.Main.reflectPrivate(Main.java:60)
[exit code 1]
Both blocks are from 04-reflection.txt; the stack trace is trimmed to the first frame that is your code. Look at the wording, because you will meet it again in logs: module com.acme.greeter does not “opens com.acme.greeter.api” to module com.acme.app. It names the module that owns the field, the package, and the module that tried. Those three names are exactly what an --add-opens flag needs:
$ java --add-opens com.acme.greeter/com.acme.greeter.api=com.acme.app -p build/mods -m com.acme.app/com.acme.app.Main reflect-private
found the field: private final java.lang.String com.acme.greeter.api.Greeter.prefix
read it: Hello
The command-line form is --add-opens owning-module/package=reading-module. It is the emergency fix for code you do not control, a library that reflects into your classes or yours reflecting into a library. For code you do control, the proper fix is an opens line in the right descriptor, which is what the next example does.
A framework needs opens, and only for itself
To make that realistic, the demo includescom.acme.mapper, a twenty-line stand-in for Jackson, Hibernate or Spring that reads every field of any object it is given. I did not run those libraries for this post; the mechanism is the same call, setAccessible(true), and that call is what the module system checks.
public static Map<String, Object> toMap(Object o) throws ReflectiveOperationException {
Map<String, Object> m = new LinkedHashMap<>();
for (Field f : o.getClass().getDeclaredFields()) {
f.setAccessible(true); // <- the call that the module system polices
m.put(f.getName(), f.get(o));
}
return m;
}
That is the heart of Mapper.java. The application’s Customer has two private fields and no getters. With the application’s descriptor you saw earlier, which says opens com.acme.app.domain to com.acme.mapper, it works. I then made a copy of the application with that one line deleted. Three blocks follow: the committed descriptor at work, the one-line diff, and the failure.
$ java -p build/mods -m com.acme.app/com.acme.app.Main mapper
{name=Asha, loyaltyPoints=120}
$ diff src/com.acme.app/module-info.java build/src-noopens/com.acme.app/module-info.java
6d5
< opens com.acme.app.domain to com.acme.mapper;
[exit code 1]
$ java -p build/mods-noopens -m com.acme.app/com.acme.app.Main mapper
Exception in thread "main" java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.lang.String com.acme.app.domain.Customer.name accessible: module com.acme.app does not "opens com.acme.app.domain" to module com.acme.mapper
at com.acme.app/com.acme.app.Main.mapper(Main.java:81)
[exit code 1]
From 05-opens-for-frameworks.txt. Note who is blamed: the exception says the application module does not open its domain package to the mapper module. That is the message you will see when a framework cannot instantiate or populate your classes after you modularise. The qualified form (to com.acme.mapper) is deliberate: it opens the domain package to the one module that needs it and to nobody else.
The fingerprint.InaccessibleObjectException: Unable to make field ... accessible: module X does not "opens P" to module Yalways means: a module named Y calledsetAccessible(true)on a private member in package P of module X, and X’s descriptor does not allow it. Fix it withopens P to Yin X (when X is yours), or--add-opens X/P=Yon the command line (when it is not). If Y says unnamed module, the caller is on the class path.
Going deeper: what else lets a caller through, and what does not
Not exported is not the same as unreachable by name. Class.forName("com.acme.greeter.internal.Secrets") succeeded; loading a class has nothing to do with accessibility. The failure came one step later, when Method.invoke checked access. If your code catches ClassNotFoundException as the signal for “internal class absent”, it will not fire here.
$ java -p build/mods -m com.acme.app/com.acme.app.Main forname-internal
class loaded: com.acme.greeter.internal.Secrets
calling it...
Exception in thread "main" java.lang.IllegalAccessException: class com.acme.app.Main (in module com.acme.app) cannot access class com.acme.greeter.internal.Secrets (in module com.acme.greeter) because module com.acme.greeter does not export com.acme.greeter.internal to module com.acme.app
at com.acme.app/com.acme.app.Main.fornameInternal(Main.java:77)
[exit code 1]
The matching command-line flag for compile-time-visible-but-not-exported packages is --add-exports owner/package=reader. It makes the call above succeed, and note that it is a different flag from --add-opens:
$ java --add-exports com.acme.greeter/com.acme.greeter.internal=com.acme.app -p build/mods -m com.acme.app/com.acme.app.Main forname-internal
class loaded: com.acme.greeter.internal.Secrets
calling it...
s3cr3t
Both blocks are from 04-reflection.txt. Other ways to open a package: put open module com.acme.app { ... } in the descriptor to open every package (convenient for application modules, discouraged for libraries), or add the Add-Opens manifest attribute to an executable JAR. JEP 403 confirms that --add-opens and the Add-Opens manifest attribute still work in JDK 17 and later; I did not test the manifest attribute here.
Java also lets code reach into modules without setAccessible via MethodHandles.privateLookupIn, which has the same opens requirement. The cost of reflection versus method handles is covered in Java Reflection and MethodHandles in 2026.
- Every runtime question the demo asks of the module system, printed from inside the JVM: 02-describe.txt (last block,
Module.isExported/isOpen) - JEP 403: Strongly Encapsulate JDK Internals
- java.lang.Module (isExported, isOpen, addOpens)
5. The JDK is made of modules too, and its internals are closed
The same rules protect the JDK from you.java.base exports java.lang and java.util, but it does not open them, so reaching into the private value field of String by reflection fails exactly like reaching into Greeter.prefix did:
static void jdkInternals() throws Exception {
Field f = String.class.getDeclaredField("value");
System.out.println("found: " + f);
f.setAccessible(true);
System.out.println("value length: " + ((byte[]) f.get("hello")).length);
}
$ java -p build/mods -m com.acme.app/com.acme.app.Main jdk-internals
found: private final byte[] java.lang.String.value
Exception in thread "main" java.lang.reflect.InaccessibleObjectException: Unable to make field private final byte[] java.lang.String.value accessible: module java.base does not "opens java.lang" to module com.acme.app
at com.acme.app/com.acme.app.Main.jdkInternals(Main.java:94)
[exit code 1]
Code is in Main.java and the output in 04-reflection.txt. This one error is behind a lot of “works on Java 8, fails on 17” reports. In JDK 9 to 15 the default mode of the --illegal-access option let the same call through (with at most a warning). JEP 396 made it an error by default in JDK 16, and JEP 403 removed the option in JDK 17. What is left is the targeted flag, --add-opens java.base/java.lang=com.acme.app, which in the transcript makes the line print value length: 5.
You can list what the JDK is made of, and describe any of it with the same command you used on your own modules:
$ java --describe-module java.sql
java.sql@25.0.5.1
exports java.sql
exports javax.sql
requires java.base mandated
requires java.transaction.xa transitive
requires java.logging transitive
requires java.xml transitive
uses java.sql.Driver
$ java --list-modules | wc -l
69
From 02-describe.txt. On this Temurin 25 build there are 69 modules. java.sql shows requires java.xml transitive and friends, the same keyword you used in greeter. A type such as java.sql.Driver exposes a java.util.logging.Logger (from java.logging) in its signature, which is exactly the situation requires transitive is for. It is the pattern working at JDK scale, and it is what makes the 69 modules a graph instead of a list.
Going deeper: the unsupported escape hatches
sun.misc.Unsafe is the notable exception: JEP 403 keeps “critical internal APIs such as sun.misc.Unsafe” accessible, in the module jdk.unsupported. Its memory-access methods are on a deprecation path of their own; how to find that in your dependencies, with jdeps and a bytecode scan of 17 real jars, is the subject of sun.misc.Unsafe Memory Access Is Going Away.
For a migration from JDK 17 to 25 in general, with the flags that are still useful, see Java 17 to Java 25 Migration Guide.
6. On the class path none of this applies, which is how you adopt modules gradually
The same compiled classes, with themodule-info.java files removed and everything put on -cp, behave very differently. run.sh copies the sources into one folder without descriptors, compiles, and runs the same scenarios. The first thing to look at is the module of the application class:
$ java -cp build/cp-classes com.acme.app.Main inspect
app.isNamed() = false name=null
greeter.isNamed() = false
app reads greeter = true
com.acme.greeter.api exported to app: true opened to app: true
com.acme.greeter.internal exported to app: true opened to app: true
String.class module = module java.base
From 07-classpath.txt. Both classes are in the unnamed module: isNamed() is false, the internal package is exported and opened to the application, and a class that was invisible a minute ago is simply reachable. The reflection and internal-access scenarios that failed on the module path now succeed:
$ java -cp build/cp-classes com.acme.app.Main forname-internal
class loaded: com.acme.greeter.internal.Secrets
calling it...
s3cr3t
$ java -cp build/cp-classes com.acme.app.Main reflect-private
found the field: private final java.lang.String com.acme.greeter.api.Greeter.prefix
read it: Hello
The JDK’s own encapsulation still holds even here, because java.base is a named module. The unnamed module is not allowed in. The error text changes its last words from the name of a module to unnamed module:
$ java -cp build/cp-classes com.acme.app.Main jdk-internals
found: private final byte[] java.lang.String.value
Exception in thread "main" java.lang.reflect.InaccessibleObjectException: Unable to make field private final byte[] java.lang.String.value accessible: module java.base does not "opens java.lang" to unnamed module @<hash>
[exit code 1]
The class-path form of the fix names ALL-UNNAMED as the reader, and it works:
$ java --add-opens java.base/java.lang=ALL-UNNAMED -cp build/cp-classes com.acme.app.Main jdk-internals
found: private final byte[] java.lang.String.value
value length: 5
Both blocks are from 07-classpath.txt.
Classpath behaviour is the default for most applications today. Strong encapsulation of your classes only exists if you put them on the module path. Strong encapsulation of the JDK applies to everyone. The practical consequence: an application that runs on the class path can still hit thejava.base does not "opens java.lang" to unnamed moduleerror above, and the fix is the--add-opens java.base/java.lang=ALL-UNNAMEDshown above, not a module descriptor.
Going deeper: what stops working when you leave modules behind
A provides ... with clause is read from a module descriptor, and on the class path there are no descriptors. The class-path mechanism is a text file, META-INF/services/<interface>; I did not test it here. In the transcript, the class-path run of the services scenario finds no provider:
$ java -cp build/cp-classes com.acme.app.Main services
providers found: 0
Greeting[text=Hello, Asha! (s3cr3t), formatter=built-in]
The module com.acme.formal was compiled into the same cp-classes folder, but without its descriptor nothing registers it as a provider. A library meant for both worlds therefore needs both registrations (section 7 shows the module one).
JEP 261 describes a related rule from the other direction: a package that is defined in a named module and also on the class path is ignored on the class path, so the class path can no longer add classes to a package the module system owns. I did not run that case; it is quoted from the JEP.
7. Services let one module use another without naming it
Sometimes the module that does the work should not know who supplies the implementation: a logging backend, a database driver, an output formatter.java.sql showed this at the end of its descriptor (uses java.sql.Driver). In the demo, greeter declares uses com.acme.greeter.api.Formatter and the separate module com.acme.formal declares the other half:
module com.acme.formal {
requires com.acme.greeter;
provides com.acme.greeter.api.Formatter with com.acme.formal.FormalFormatter;
}
The provider is in module-info.java. Its class, FormalFormatter, is not exported at all: the only way to reach it is ServiceLoader.load(Formatter.class), which Greeter.java calls. Greeter has no requires com.acme.formal and no import. Run it:
$ java -p build/mods -m com.acme.app/com.acme.app.Main services
providers found: 1
Greeting[text=Good day, Asha., formatter=formal]
From 08-services.txt. A provider is picked up just by being on the module path, which is the point and also the surprise: I expected to need --add-modules com.acme.formal, and I did not. The JVM resolves the root module, its dependencies, and then binds any observable module that provides a service one of the resolved modules uses. --show-module-resolution prints the word binds for exactly that step:
$ java -p build/mods --show-module-resolution -m com.acme.app/com.acme.app.Main services | grep -v '^root\|^java\.\|^jdk\.'
com.acme.app requires com.acme.greeter file:///.../jpms-basics/build/mods/com.acme.greeter/
com.acme.app requires com.acme.mapper file:///.../jpms-basics/build/mods/com.acme.mapper/
com.acme.greeter requires com.acme.model file:///.../jpms-basics/build/mods/com.acme.model/
com.acme.greeter binds com.acme.formal file:///.../jpms-basics/build/mods/com.acme.formal/
com.acme.formal requires com.acme.greeter file:///.../jpms-basics/build/mods/com.acme.greeter/
providers found: 1
Greeting[text=Good day, Asha., formatter=formal]
And when the provider is removed from the path, the application quietly falls back to its default:
$ java -p build/mods-noformal -m com.acme.app/com.acme.app.Main services
providers found: 0
Greeting[text=Hello, Asha! (s3cr3t), formatter=built-in]
No error, different behaviour. A missing provider is not an exception:ServiceLoaderjust finds zero. Here, one provider means “Good day, Asha.” and none means “Hello, Asha!”. In production this looks like a feature that disappeared after a repackaging. The same thing appears in section 11, wherejlinkstrips the provider out of a runtime image unless you ask it not to.
Going deeper: services in practice
Two rules worth knowing. First, uses is required in the consuming module’s descriptor or ServiceLoader.load throws ServiceConfigurationError; I did not provoke that error here. Second, a provider class needs a public no-arg constructor or a public static provider() method; the demo uses the constructor.
JDBC drivers are discovered this way: the java.sql descriptor printed in section 5 ends with uses java.sql.Driver. The companion project does not load a real driver; the point is that the mechanism you just used is the one the JDK itself uses for an extension point.
8. One package, one module: the split-package error
A package may live in only one module. Two JARs that both contain, say,com.acme.shared are fine on the class path, which is how a lot of old libraries ended up sharing a package name with their “extras” JAR. Put both on the module path and the module system refuses. The demo has two modules, split.a and split.b, each with one class in com.acme.shared, and a third that requires both (broken/05-split/).
$ javac -d build/split-all --module-source-path broken/05-split --module split.a,split.b && echo "split.a and split.b compiled"
split.a and split.b compiled
Compiling the two on their own works: nothing reads both, so nothing is ambiguous yet. The error appears the moment one module reads both:
$ javac -d build/split/splitapp -p build/split $(find broken/05-split/splitapp -name "*.java")
broken/05-split/splitapp/module-info.java:1: error: module splitapp reads package com.acme.shared from both split.a and split.b
module splitapp { requires split.a; requires split.b; }
^
1 error
[exit code 1]
From 06-split-packages.txt. There is also a run-time form for the case where no module reads both, but the JVM would put them in the same class loader. The boot layer uses one application class loader for all modules on the module path, so even unrelated modules cannot share a package name:
$ java -p build/split --add-modules split.a,split.b -m split.a/com.acme.shared.PartA
Error occurred during initialization of boot layer
java.lang.LayerInstantiationException: Package com.acme.shared in both module split.a and module split.b
[exit code 1]
Note the error is a LayerInstantiationException at boot, not at the first use of the class, so the application never starts. The same three source trees compiled without descriptors and run on the class path work, and show both classes living in the unnamed module:
$ java -cp build/split-cp com.acme.splitapp.Main
com.acme.shared.PartA from unnamed module @<hash>
com.acme.shared.PartB from unnamed module @<hash>
Split packages are why some libraries cannot simply be modularised. If two of your own JARs share a package, you must move classes before either can have amodule-info.java. If two third-party JARs share one, they can only live on the class path (or you merge them with a build step). Compare thecontainslines thatjar --describe-moduleprints for each JAR (section 9 shows them) to find shared packages before you start.
Going deeper: finding split packages before they bite
The javac message above names both modules, which makes it easy. The harder case is a split between your module and a library in the unnamed module or an automatic module; the module system ignores the class-path copy of a package that a named module owns (JEP 261), so classes you can see in the JAR may be missing at run time instead of producing a split-package message. I did not reproduce that variant, so treat it as the JEP’s statement, not as measured.
- JEP 261 (package-visibility rules)
- Run
jar --describe-module --file x.jaron the JARs involved and compare thecontainslines
9. Old jars become “automatic modules” when you put them on the module path
Your own modules will not be the only thing on the module path. Most libraries do not have a descriptor, or only recently got one. A JAR with nomodule-info.class that you place on the module path becomes an automatic module: the module system builds a descriptor for it on the fly. To try it, the demo includes a deliberately old-fashioned library, Util.java, compiled and zipped into legacy-lib-1.2.jar.
$ jar --describe-module --file build/libs/legacy-lib-1.2.jar
No module descriptor found. Derived automatic module.
legacy.lib@1.2 automatic
requires java.base mandated
contains com.legacy.lib
From 09-automatic-modules.txt. The name legacy.lib and the version 1.2 were derived from the file name: the .jar suffix is removed, a trailing -1.2 becomes the version, and non-alphanumeric characters in the rest become dots (the rules are in the ModuleFinder Javadoc). Rename the JAR and the module name changes with it, and every requires that mentioned it breaks. A module can use an automatic one by that derived name:
module com.acme.autoapp {
requires legacy.lib; // the name the module system derived from legacy-lib-1.2.jar
}
That is module-info.java. At run time, what does the module system let it do?
$ java -p build/autoapp:build/libs/legacy-lib-1.2.jar -m com.acme.autoapp/com.acme.autoapp.Main
LEGACY!
module legacy.lib automatic=true
descriptor exports = []
isExported(com.legacy.lib) = true, isOpen = true
legacy.lib reads com.acme.autoapp = true
The descriptor lists no exports (descriptor exports = []), yet isExported and isOpen are both true: an automatic module exports and opens all its packages and reads every other module. The Java SE 25 java.lang.module documentation states the “reads everything” part; the exports and opens behaviour is what this run printed. That generosity is a bridge, not a destination: it gives no encapsulation at all.
The one line library authors should add
If your library is not ready for a real descriptor, you can still give it a stable name by adding a manifest entry. The demo builds a second JAR withAutomatic-Module-Name: com.legacy.lib:
$ unzip -p build/libs-named/legacy-lib-1.3.jar META-INF/MANIFEST.MF
Manifest-Version: 1.0
Automatic-Module-Name: com.legacy.lib
Created-By: 25.0.5.1 (Eclipse Adoptium)
$ java -p build/libs-named --describe-module com.legacy.lib
com.legacy.lib@1.3 file:///.../jpms-basics/build/libs-named/legacy-lib-1.3.jar automatic
requires java.base mandated
contains com.legacy.lib
The version 1.3 still comes from the file name, but the module name now comes from the manifest and no longer depends on how the file is named. Libraries that publish Automatic-Module-Name let their users write a requires that survives a rename of the artifact.
Automatic modules and jlink do not mix.jlinkrefuses to build an image that includes an automatic module. The exact message is in section 11. If you want a jlink runtime and still depend on a descriptor-less library, you need to give that library a real descriptor (jdeps --generate-module-info, shown next) or leave it out of the image.
Going deeper: reading the compiler warning for the legacy jar
The legacy library names sun.misc.Unsafe in a class literal, to give jdeps something to report in the next section. Compiling it prints the warning below, which is why run.sh saves it to its own file:
legacy/src/com/legacy/lib/Util.java:8: warning: Unsafe is internal proprietary API and may be removed in a future release
public static String unsafeClassName() { return sun.misc.Unsafe.class.getName(); }
^
1 warning
From 09a-legacy-javac-warnings.txt. This is the compile-time counterpart of the run-time restrictions: jdk.unsupported is still available on JDK 25, which is why this compiles, and it is what jdeps --jdk-internals exists to find.
10. jdeps shows what depends on what, before you write a single module-info
jdeps ships with the JDK. It reads class files (or modules, or JARs) and tells you which packages and modules they use. It answers the questions you need answered before modularising: what does my jar need from the JDK, does it use JDK internals, and what would a descriptor for it look like?
On a module you wrote
$ jdeps --module-path build/mods -summary --module com.acme.app
com.acme.app -> com.acme.greeter
com.acme.app -> com.acme.mapper
com.acme.app -> com.acme.model
com.acme.app -> java.base
$ jdeps --module-path build/mods --check com.acme.greeter
com.acme.greeter (file:///.../jpms-basics/build/mods/com.acme.greeter/)
[Module descriptor]
requires transitive com.acme.model;
requires mandated java.base (@25.0.5.1);
[Suggested module descriptor for com.acme.greeter]
requires transitive com.acme.model;
requires mandated java.base;
[Transitive reduced graph for com.acme.greeter]
requires transitive com.acme.model;
requires mandated java.base;
From 10-jdeps.txt. The first is the summary: who depends on whom, with com.acme.model showing up even though the application never wrote a requires for it. --check compares your descriptor to what the code actually uses and prints a suggested descriptor; in this case they agree. It would also flag unused qualified exports.
On an old jar
$ jdeps --list-deps build/libs/legacy-lib-1.2.jar
java.base
jdk.unsupported
$ jdeps --jdk-internals build/libs/legacy-lib-1.2.jar
legacy-lib-1.2.jar -> jdk.unsupported
com.legacy.lib.Util -> sun.misc.Unsafe JDK internal API (jdk.unsupported)
The library needs java.base and jdk.unsupported, and the second command points at the exact class and the internal API it reaches (the full warning text is in the transcript). When the library is yours, this is the list to fix before upgrading JDKs.
Finally, --generate-module-info writes a first draft of a descriptor, which is the quickest way to start modularising a legacy jar:
$ jdeps --generate-module-info build/generated build/libs/legacy-lib-1.2.jar && cat build/generated/legacy.lib/module-info.java
writing to build/generated/legacy.lib/module-info.java
module legacy.lib {
requires jdk.unsupported;
exports com.legacy.lib;
}
$ jdeps --print-module-deps build/libs/legacy-lib-1.2.jar
java.base,jdk.unsupported
The last two blocks are also from 10-jdeps.txt; the draft descriptor is generated, not hand-written.
--print-module-deps prints a comma-separated list that jlink --add-modules accepts, which is the bridge between a class-path application and a custom runtime.
A generated descriptor is a draft, not a design.--generate-module-infoexportedcom.legacy.libbecause it had no way to know whether that was intended; it cannot tell which packages are meant to be public. Read it and remove exports before you commit it.
Going deeper: which jdeps flag for which question
| Question | Command |
|---|---|
| Which JDK modules does this jar need? | jdeps --print-module-deps app.jar or --list-deps |
| Does it use JDK internal APIs? | jdeps --jdk-internals app.jar |
| What would a descriptor look like? | jdeps --generate-module-info outdir lib.jar |
| Is my descriptor right? | jdeps --module-path mods --check my.module |
| Who depends on whom? | jdeps -summary (add -s for short form) |
These descriptions follow the jdeps reference for Java SE 25; each one was run in the transcript above except --list-reduced-deps and --dot-output, which I did not run. Two limits from the reference: --jdk-internals cannot be combined with -p, -e or -s, and --generate-module-info cannot be combined with --class-path. jdeps reads bytecode statically, so a dependency that exists only through reflection is not something I would expect it to report; I did not test that.
- jdeps reference (Java SE 25)
jdeprscanandjdepson a real migration: Java 17 to Java 25 Migration Guide
11. jlink builds a runtime with only the modules you use
The reward for describing your dependencies is that the JDK can use them.jlink takes your modules plus the JDK modules they need and writes a self-contained runtime folder with its own java. Nothing else from the JDK comes along.
$ jlink --module-path build/mods:$JMODS --add-modules com.acme.app --launcher acme=com.acme.app/com.acme.app.Main --strip-debug --no-header-files --no-man-pages --output build/image-plain
$ build/image-plain/bin/java --list-modules
com.acme.app
com.acme.greeter
com.acme.mapper
com.acme.model
java.base@25.0.5.1
From 11-jlink.txt. $JMODS in the command is the folder of JDK .jmod files (the jlink reference says the default is $JAVA_HOME/jmods); the Temurin 25 tarball I used does not contain one, so I downloaded the matching Temurin jmods package. That is the first of two jlink surprises, and it has its own transcript, 11b-jlink-without-jmods.txt. The runtime holds exactly five modules: my four and java.base. --launcher acme=... generated a start script, and the sizes are:
--compress zip-9 brought it to 43,176 KB. That is the point of jlink for a container image. The orange bar is the second surprise.
The surprise: services are not bound by default
The first image above does not containcom.acme.formal. Nothing in the application requires it, so jlink did not pull it in, and the runtime behaves as if the provider did not exist:
$ build/image-plain/bin/acme services
providers found: 0
Greeting[text=Hello, Asha! (s3cr3t), formatter=built-in]
This is the same silent difference as in section 7, reproduced in a runtime image. There are two fixes. Name the provider explicitly, which is what I recommend:
$ build/image-explicit/bin/acme services
providers found: 1
Greeting[text=Good day, Asha., formatter=formal]
(11-jlink.txt, as are the next two blocks.)
Or ask jlink for --bind-services, which links in every provider of every service used by any module in the image. It works, and it is the orange bar in the chart:
$ build/image-bound/bin/java --list-modules | grep -E "jlink|jdeps|jdk.compiler"
jdk.compiler@25.0.5.1
jdk.jdeps@25.0.5.1
jdk.jlink@25.0.5.1
$ echo "modules in image-plain: $(build/image-plain/bin/java --list-modules | wc -l), in image-bound: $(build/image-bound/bin/java --list-modules | wc -l)"
modules in image-plain: 5, in image-bound: 49
Service binding pulled in 44 extra JDK modules, including the compiler and jlink itself, because the JDK’s own modules use services and the providers for those were linked in too. That is my reading of the result; the jlink reference only says the option links “service provider modules and their dependencies”. The image grew to 166,088 KB, roughly three times the explicit version, for a program that needed 5 modules.
Recommended: list providers with –add-modules.--bind-servicesis convenient but unselective. On this JDK build it turned a 5-module image into a 49-module one. And if your JDK has nojmodsfolder (the JEP 493 style of linking directly from the run-time image), the option fails outright (11b-jlink-without-jmods.txt):JEP 493 documents this exact error as a restriction: a JDK without$ jlink --module-path build/mods --add-modules com.acme.app --bind-services --launcher acme=com.acme.app/com.acme.app.Main --strip-debug --no-header-files --no-man-pages --output build/image-nojmods Error: This JDK does not contain packaged modules and cannot be used to create another image with the jdk.jlink module [exit code 1]jmodscannot create an image that includes thejdk.jlinkmodule. That fits what the previous section showed,jdk.jlinkarriving with service binding. In the same transcript, naming the provider explicitly still works on that JDK.
The other limit: automatic modules
$ jlink --module-path build/mods:$JMODS:build/libs --add-modules com.acme.app,legacy.lib --output build/image-auto
Error: automatic module cannot be used with jlink: legacy.lib from file:///.../jpms-basics/build/libs/legacy-lib-1.2.jar
[exit code 1]
As promised in section 9: the legacy JAR cannot go into the image. ($JMODS in the transcript is the folder of JDK modules.)
Going deeper: jlink options worth knowing
| Option | What it does | In this post |
|---|---|---|
--add-modules | root modules; their dependencies come along | yes |
--launcher name=module/class | creates bin/name | yes |
--strip-debug, --no-header-files, --no-man-pages | smaller image | yes |
--compress zip-9 | compress resources | yes: 56,752 to 43,176 KB |
--bind-services | link service providers and their dependencies | yes: the trap above |
An image built this way is tied to the platform and the JDK it was linked from, and it does not update itself with JDK security patches: you relink and redeploy. A complete walk through building one for a Spring Boot application, with measured sizes, is in jlink and JMOD: Cut a Spring Boot Java Runtime from 303 MB to 65 MB; this post deliberately stays with a small modular application so the rules are visible.
12. Should you modularise your application?
For a library you publish: add Automatic-Module-Name today, a real descriptor when you can. It costs one manifest line and saves your users from a name that changes with the artifact file name. For an application: usually not yet. If it runs fine on the class path, the module system is already helping you, because the JDK is encapsulated either way, and the errors in sections 4 and 5 are fixed withA reasonable order of work, if you do decide to start: run--add-opens, not a rewrite. Modularise an application when you need one of three things: to build a small runtime withjlink, to enforce internal boundaries between teams at compile time, or to run on the module path because a dependency requires it. I did not run a Spring Boot application on the module path for this post, so I am not making claims about that combination. What you do need is to read the errors: you now have the fingerprint of each.
jdeps --jdk-internals on the application and its libraries; fix or flag whatever it reports; put the application on the class path of the current JDK and watch for InaccessibleObjectException; add --add-opens for what remains; and only then consider descriptors.
Production checklist
- Run
jdeps --jdk-internalsover every JAR you ship and every third-party JAR you depend on, before changing JDK versions - Treat
InaccessibleObjectExceptionas a configuration error to fix once, with a documented--add-opens owner/package=reader, not as a reason to disable checks - Keep
--add-opensflags in one place (a launcher script, aJAVA_TOOL_OPTIONSvariable or anAdd-Opensmanifest) so they can be removed when the library is upgraded - Prefer
opens p to specific.moduleoveropens p, and overopen module, for anything that is not an application-private module - In library modules, put every type that appears in a public signature behind
requires transitive - Publish
Automatic-Module-Namein every JAR you release, even before you write a real descriptor; never rename it later - Check for split packages across your own JARs before adding the first
module-info.java - For
jlinkimages, list service providers in--add-modulesand compare--list-modulesto what you expect; avoid--bind-servicesunless you have measured it - Rebuild the
jlinkruntime on every JDK security update; it does not patch itself - Re-run
run.shon the JDK you deploy: error wording and defaults change between releases, and every line in this post is only as true as that run
Further reading
- Companion code and every transcript: asmhatre/javademos/jpms-basics
- jlink and JMOD: Cut a Spring Boot Java Runtime from 303 MB to 65 MB
- sun.misc.Unsafe Memory Access Is Going Away: Find and Fix It in Your Dependencies
- Java 17 to Java 25 Migration Guide: The Direct Jump
- Java Reflection and MethodHandles in 2026
- JDK vs JRE vs JVM
- JEP 261: Module System
- JEP 396: Strongly Encapsulate JDK Internals by Default
- JEP 403: Strongly Encapsulate JDK Internals
- JEP 493: Linking Run-Time Images without JMODs
- jdeps (Java SE 25)
- jlink (Java SE 25)
No Comments yet!