Strong Encapsulation of JDK Internals in Java 17
Since Java 17 (JEP 403), the internal classes and private members of the JDK are strongly encapsulated. Code can no longer use deep reflection, such as setAccessible(true), on JDK internals, and the --illegal-access option that relaxed this rule has no effect any more.
This tutorial shows the error you will see, why the change was made, and how to fix or temporarily work around it.
1. Why Was This Change Made?
The module system in Java 9 separated the supported APIs of the JDK from its internals. For compatibility, Java 9 to 15 still allowed deep reflection into internals with a warning. Libraries that rely on internals break when the JDK changes, and internals can expose security risks. Java 16 (JEP 396) denied such access by default, and Java 17 removed the switch that could re-enable it.
Critical internal APIs such as sun.misc.Unsafe remain available through the jdk.unsupported module, because there were no public replacements yet.
2. The Options That Matter
| Option | Effect in Java 17 |
|---|---|
--illegal-access=permit | ignored with a warning; support removed |
--add-opens module/package=target | allows deep reflection into one package |
--add-exports module/package=target | allows compile-time and run-time access to public types of an internal package |
Add-Opens: in a JAR manifest | same as --add-opens for executable JARs |
3. Practical Examples
Example 1: Deep reflection is blocked
import java.lang.reflect.Field;
import java.lang.reflect.InaccessibleObjectException;
import java.util.ArrayList;
public class DeepReflection {
public static void main(String[] args) throws Exception {
ArrayList<String> list = new ArrayList<>();
list.add("a");
// Reading a private field of a JDK class with reflection
Field elementData = ArrayList.class.getDeclaredField("elementData");
try {
elementData.setAccessible(true);
Object[] array = (Object[]) elementData.get(list);
System.out.println("Internal capacity: " + array.length);
} catch (InaccessibleObjectException e) {
String msg = e.getMessage();
System.out.println("Blocked: " + msg.substring(0, msg.indexOf(" to unnamed module")));
}
}
}
Blocked: Unable to make field transient java.lang.Object[] java.util.ArrayList.elementData accessible: module java.base does not "opens java.util"
Compile and run: java DeepReflection
Example 2: The old --illegal-access option is ignored
Command: java --illegal-access=permit DeepReflection
Blocked: Unable to make field transient java.lang.Object[] java.util.ArrayList.elementData accessible: module java.base does not "opens java.util" Java HotSpot(TM) 64-Bit Server VM warning: Ignoring option --illegal-access=permit; support was removed in 17.0
The JVM prints a warning that the option was removed in 17.0, and the access is still denied. This output came from an Oracle JDK 21 runtime, which prints the same message.
Example 3: Opening one package explicitly
Command: java --add-opens java.base/java.util=ALL-UNNAMED DeepReflection
Internal capacity: 10
4. How Access Is Decided
5. Important Points and Best Practices
- Upgrade libraries first; most popular frameworks stopped using JDK internals long ago.
- Use
jdeps --jdk-internals your.jarto find dependencies on internal APIs. - Treat
--add-opensas a temporary workaround and open only the packages you need. - Replace internal APIs with supported ones, for example
VarHandleinstead of manyUnsafeuses.
6. Summary
- Java 17 (JEP 403) strongly encapsulates JDK internals.
- Deep reflection into JDK packages throws
InaccessibleObjectException. --illegal-accessno longer has any effect.--add-opensopens specific packages as a temporary workaround.
Related Tutorials
- Removed Java EE and CORBA Modules (Java 11)
- Java 17 Features
- Sealed Classes
- Helpful NullPointerExceptions
7. References
Frequently Asked Questions (FAQ)
Q1. What is InaccessibleObjectException?
An unchecked exception thrown when setAccessible(true) is called on a member of a package that its module does not open to the caller.
Q2. Does --illegal-access still work in Java 17?
No. Java 17 removed it; the JVM prints a warning and ignores the option.
Q3. How can I allow reflection into a JDK package?
Add --add-opens java.base/java.util=ALL-UNNAMED (with the right module and package) to the java command, or an Add-Opens entry to the JAR manifest.
Q4. Is sun.misc.Unsafe still available in Java 17?
Yes, through the jdk.unsupported module, because it is a critical internal API without a full replacement.