Quick revision
The short answer
Java 9 put the JDK into modules, but until JDK 16 the JVM still allowed reflective access into JDK internals with a warning. JDK 16 made strong encapsulation the default and JDK 17 removed the --illegal-access option, so setAccessible(true) on a non-public member of a JDK package that is not opened to the caller now throws InaccessibleObjectException. The correct fix is to upgrade the library to a version that uses supported APIs; the short-term workaround is --add-opens java.base/java.lang=ALL-UNNAMED, documented and tracked for removal, never added broadly to every service.
What to remember
In this answer, we saw that Java 9 introduced modules but kept deep reflection into the JDK working with a warning, JDK 16 made strong encapsulation the default, and JDK 17 removed --illegal-access, so setAccessible on a member of a package not opened to the caller now throws InaccessibleObjectException. We read the message to find the package and the library, fixed it properly by upgrading the library, used a single documented --add-opens java.base/java.lang=ALL-UNNAMED only as a tracked short-term workaround, and avoided broad flags that hide future breakage.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Java 9 put the JDK into modules, but until JDK 16 the JVM still allowed reflective access into JDK internals with a warning. JDK 16 made strong encapsulation the default and JDK 17 removed the --illegal-access option, so setAccessible(true) on a non-public member of a JDK package that is not opened to the caller now throws InaccessibleObjectException. The correct fix is to upgrade the library to a version that uses supported APIs; the short-term workaround is --add-opens java.base/java.lang=ALL-UNNAMED, documented and tracked for removal, never added broadly to every service.
Why this question is asked
Upgrading from Java 8 straight to 17 skips nine releases of changes, and this exception is the one almost every team meets. It looks alarming because it names a JDK class and a module, but it is not a JDK bug: some library is reaching into private parts of the JDK that it was never supposed to touch. Interviewers ask this because the right answer separates a quick workaround from a real fix and shows that we will not hide the problem across a fleet. In this answer, we will recap what changed between Java 8 and 17, read the error message, find the library responsible, apply the real fix and the temporary workaround, and see why broad flags are harmful.
1A Quick Recap: What Changed Between Java 8 and 17
In Java 8 there were no modules. Any code could call setAccessible(true) on any private field or method, including the internals of java.lang.String or java.lang.ClassLoader, and many libraries did, usually for speed or to use features the JDK did not expose publicly.
Java 9 introduced the module system and split the JDK into modules such as java.base. A module only opens its packages for deep reflection, the kind that reaches private members, if it says so, and java.base opens none of its internals. To avoid breaking everyone at once, Java 9 to 15 ran with a relaxed setting, --illegal-access=permit, which allowed such access and printed a warning the first time. Many teams saw lines like WARNING: An illegal reflective access operation has occurred, and ignored them.
JDK 16, through JEP 396, changed the default to deny. JDK 17, through JEP 403, removed the --illegal-access option entirely, so it can no longer be turned back on. The warning from Java 11 became an exception in Java 17. Teams that jump from 8 to 17 never saw the warnings at all; the failure is their first notice.
| Release | Default | Effect of setAccessible on JDK internals |
|---|---|---|
| Java 8 | No modules | Always works |
| Java 9 to 15 | --illegal-access=permit | Works, warns once |
| Java 16 | --illegal-access=deny | Throws, option can still relax it |
| Java 17 and later | Option removed | Throws unless the package is opened |
2Reading the Error Message
The message tells us almost everything we need. Here is a typical one, with the stack trace trimmed to the important frames.
Read it in three parts. The member: the private final field value of java.lang.String. The rule: the module java.base does not open the package java.lang. The caller: the unnamed module, which is where everything on the class path lives, so it is our application or one of its jars. The stack trace then names the culprit, the first frame below Field.setAccessible that is not JDK code. Here it is a serialisation library trying to read String's internal byte array directly, a trick that was a small speed-up years ago.
3The Best Fix: Upgrade the Library
Almost every widely used library that did this has released versions that use supported APIs instead: public methods, MethodHandles, VarHandles, or a fallback when access is denied. Because the stack trace names the library, the fix is usually a dependency upgrade, which also brings support for the newer JDK's class files and other changes made since the old version was released.
When no fixed version exists, because the library is abandoned, the real fix is to replace it. That is more work, but a library that depends on private JDK internals will break again on later releases, so keeping it is only postponing the cost.
4The Short-Term Workaround: --add-opens
If the upgrade cannot happen today, we can open the specific package to the class path with a JVM option. The format is --add-opens module/package=target, and ALL-UNNAMED means all code on the class path.
- Read the module, package and callerfrom the message and stack trace
- Is a fixed library version available?YesUpgrade it and remove the needNoAdd one narrow --add-opens
- Record owner, reason and ticketnext to the flag
- Remove the flag after the upgradeand test without it
4 steps in order. Play them, or step through them one at a time.
This restores the Java 8 behaviour for exactly one package. We open only the package named in the error, java.lang here, not everything, and we treat the flag as a debt: a comment or ticket next to it saying which library needs it, which version fixes it, and who owns the removal. Notice that the Add-Opens manifest attribute only works for the jar given to java -jar, not for libraries on the class path.
5Why Not Add Broad Flags to Every Service
A tempting shortcut is to put a long list of --add-opens flags, java.lang, java.util, java.io, sun.nio.ch and more, into the shared base image so that no service ever sees this error again. It feels efficient, but it hides exactly the information we need. Each flag lets every library in every service reach into that package, including libraries we add later, so the next library that relies on JDK internals slips in unnoticed. When a future JDK changes those internals, the failure is no longer a clear exception at startup but a strange bug inside some library.
The flags also stop telling us anything. Nobody can say which services still need which package, so nobody can ever remove them. The better policy is narrow flags added per need, recorded with a reason, and reviewed on every JDK upgrade by running the tests without them.
6Follow-up questions
How do you find all add-opens flags used across a fleet?
Two ways, and I use both. Statically, search every repository and deployment configuration for add-opens and Add-Opens: Dockerfiles, Helm charts and Kubernetes manifests, JAVA_TOOL_OPTIONS and JDK_JAVA_OPTIONS settings, start scripts, jar manifests and test JVM arguments in Maven and Gradle builds. At run time, each JVM can report its real arguments through RuntimeMXBean.getInputArguments(), which we can expose as a metric or an info endpoint, or read with jcmd <pid> VM.command_line; that catches flags injected by environment variables that no repository shows.
Why did it work on Java 11 with only a warning?
Java 11 ran with --illegal-access=permit by default, which allowed deep reflection into JDK packages that existed in Java 8 and printed one warning for the first access. JDK 16 changed the default to deny and JDK 17 removed the option, so the same code now throws.
Does sun.misc.Unsafe need --add-opens too?
No. sun.misc.Unsafe lives in the jdk.unsupported module, which exports and opens sun.misc deliberately so existing libraries kept working. Its memory access methods are instead being deprecated and restricted through their own process, controlled by a separate JVM option.
7Conclusion
In this answer, we saw that Java 9 introduced modules but kept deep reflection into the JDK working with a warning, JDK 16 made strong encapsulation the default, and JDK 17 removed --illegal-access, so setAccessible on a member of a package not opened to the caller now throws InaccessibleObjectException. We read the message to find the package and the library, fixed it properly by upgrading the library, used a single documented --add-opens java.base/java.lang=ALL-UNNAMED only as a tracked short-term workaround, and avoided broad flags that hide future breakage.
Open with
Between Java 8 and 17 the JDK became modular and strongly encapsulated. In Java 9 to 15, deep reflection into JDK internals still worked by default with an illegal reflective access warning.
Then, if they want more
JDK 16 switched the default to deny, and JDK 17 removed the --illegal-access option, so now setAccessible on a private member of a JDK package that isn't opened to the caller throws InaccessibleObjectException.
The message tells me what's going on: here, some code on the class path, the unnamed module, tried to make a private field in java.lang accessible, and java.base doesn't open java.lang. The stack trace names the library doing it, usually an old serialiser or bytecode library.
The right fix is to upgrade that library to a version that uses supported APIs; most popular ones have. If it's abandoned, replace it. As a short-term workaround, I'd add --add-opens java.base/java.lang=ALL-UNNAMED for just that package, with a comment or ticket saying which library needs it and when it goes away.
What I wouldn't do is put a long list of add-opens into every service's base image. That hides which library needs what, lets new libraries rely on internals unnoticed, and turns the next JDK change into a mysterious bug. And on each JDK upgrade, I'd test with the flags removed.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 17, Java 16, Java 8, Java 9
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "After upgrading from Java 8 to 17, a library fails with InaccessibleObjectException...": https://scrivio-interview-library.netlify.app/java/reflection-and-method-handles/529e946f67/