Quick revision
The short answer
The error means class X in module a refers to class Y in com.b.impl, a package that module b deliberately keeps internal by not exporting it. The best fix is to use b's exported API instead. If a really needs that package and b's owner agrees, b can add a qualified export, exports com.b.impl to a. As a temporary run-time workaround, --add-exports b/com.b.impl=a on the java command line lets it start. What we should avoid is an unqualified exports com.b.impl, because that turns an internal package into public API for every module.
What to remember
In this answer, we saw that the IllegalAccessError means module a uses a package module b deliberately did not export, usually after compile-time and run-time configurations diverged. We ranked the fixes: use b's exported API, or ask for one; if b's owner agrees, add a qualified exports com.b.impl to a; as a temporary run-time workaround, launch with --add-exports b/com.b.impl=a and track removing it; and avoid an unqualified export, which would turn an internal package into public API.
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.
The error means class X in module a refers to class Y in com.b.impl, a package that module b deliberately keeps internal by not exporting it. The best fix is to use b's exported API instead. If a really needs that package and b's owner agrees, b can add a qualified export, exports com.b.impl to a. As a temporary run-time workaround, --add-exports b/com.b.impl=a on the java command line lets it start. What we should avoid is an unqualified exports com.b.impl, because that turns an internal package into public API for every module.
Why this question is asked
This error usually appears after something changed: a dependency was upgraded to a version with a module descriptor, or an application moved from the class path to the module path. Code that ran for years suddenly refuses to start, and the quickest fix found online is to add a command-line flag and move on. Interviewers ask this question to see whether we understand what the error is protecting, and whether we choose a fix that respects the other module's design. In this answer, we will recap why the error happens at run time, list the fixes in order of preference with the reason for each, and see how to apply the temporary workaround at compile and run time.
1A Quick Recap: What the Error Is Saying
In the module system, a package is accessible to other modules only if its module exports it. Module b exports some packages, its API, and keeps others, such as com.b.impl, internal. Our module a reads b, but that only gives it access to b's exported packages. Class X in a refers directly to class Y in com.b.impl, so the JVM refuses the access.
Notice that the error is IllegalAccessError, a linkage error thrown by the JVM when it resolves the reference in X's bytecode, not a compile error. That tells us X was compiled in a setting where the package was visible: perhaps against an older version of b without a descriptor, on the class path where everything is visible, or with a compiler flag. The code compiled fine; the run-time configuration is now stricter than the compile-time one. Reflective access to the same class would fail differently, with an IllegalAccessException or InaccessibleObjectException, but for the same reason.
So the question is not how to silence the error, but who should decide whether com.b.impl is used by other modules. The answer is b's owners, which shapes the order of the fixes.
2Fix 1, Preferred: Use b's Exported API
Module b did not export com.b.impl on purpose. Internal packages are where a module keeps the code it wants to change freely: refactoring, renaming, replacing implementations without telling anyone. If a depends on them, every release of b can break a. So the first step is to look at what X actually needs from Y, and find the equivalent in b's exported API.
Often the exported API already covers the need, and the internal class was used out of habit or because an IDE suggested it. If the API does not offer what we need, the right move is to ask b's owners to add it to the API, a small public method or interface designed for this use. That takes longer than a flag, but the result is supported and stable.
3Fix 2: A Qualified Export, With the Owner's Agreement
Sometimes a and b are closely related, perhaps owned by the same team, and a legitimately needs b's internals, for example a testing or tooling module. Then b's owner can grant access to exactly that module with a qualified export.
The qualified form keeps the package internal for everyone else: other modules still get the same error. The JDK uses this pattern widely, sharing internal packages between its own modules without exposing them to applications. The key condition is that b's owner agrees and accepts that a now depends on internals; a qualified export is a deliberate, documented contract between two modules, not a workaround.
4Fix 3, Temporary: --add-exports at Run Time
When we cannot change b, perhaps it is a third-party library, and we need the application running today, the launcher can add the export without touching any descriptor.
The syntax is module/package=target-module, and ALL-UNNAMED as the target means all class path code. This works, but it has real costs: the flag must be carried into every launch script, test configuration and container image, it hides a dependency on internals that the next upgrade of b can break without warning, and it is easy to forget why it is there. So it should come with a ticket and a plan to move to fix 1 or fix 2.
5What to Avoid: An Unqualified Export
The tempting change, if we control b, is to write exports com.b.impl and be done. That is a much bigger decision than it looks. An unqualified export makes the package public API for every module in the world that uses b. From then on, any change to Y can break unknown consumers, and the package can never become internal again without a breaking release. It throws away exactly the encapsulation the module system gave us. If the package really should be public, it deserves to be designed and named as API, typically moved into an api package, rather than exported as impl.
- Does b's exported API offer the capability?YesFix 1: use the APINoAsk b's owners to add it
- Can b's owners add it to the API soon?YesFix 1 once releasedNoNext question
- Is a closely related to b and the owner agrees?YesFix 2: exports com.b.impl to aNoNext question
- Fix 3: --add-exports b/com.b.impl=atemporary, with a ticket
- Never: unqualified exports com.b.implinternal becomes public API
5 steps in order. Play them, or step through them one at a time.
Prefer fixes that keep b's owners in control of b's API.
6Follow-up questions
Why did this compile but fail at run time?
Because X was compiled where com.b.impl was visible, for example against b on the class path or an older b without a descriptor, and now runs on the module path against a b that does not export it. The JVM checks access when it links X's reference to Y, so the mismatch shows up as IllegalAccessError at run time.
Is --add-opens the same as --add-exports?
No. --add-exports gives compile-time and run-time access to public types, which is what direct references like X's need. --add-opens gives deep reflective access, which frameworks need for private members. For this error, --add-exports is the right flag.
How do we stop this from happening again?
Add a check in the build: compile and test on the module path the same way the application runs, and add an ArchUnit or jdeps check that fails when our code depends on packages that are not exported by their modules. jdeps --jdk-internals does the same for JDK internals.
7Conclusion
In this answer, we saw that the IllegalAccessError means module a uses a package module b deliberately did not export, usually after compile-time and run-time configurations diverged. We ranked the fixes: use b's exported API, or ask for one; if b's owner agrees, add a qualified exports com.b.impl to a; as a temporary run-time workaround, launch with --add-exports b/com.b.impl=a and track removing it; and avoid an unqualified export, which would turn an internal package into public API.
Open with
The error means class X in module a references class Y in com.b.impl, but module b does not export that package, so it is b's internal code. It is an IllegalAccessError, a linkage error, so the code was compiled somewhere the package was visible, like the class path or an older version of b, and the run-time module configuration is stricter.
Then, if they want more
I would fix it in this order. First, use b's exported API instead; internal packages are not exported precisely so b's owners can change them freely, and depending on them means any release of b can break us. If the API lacks what we need, I would ask b's owners to add it.
Second, if a is closely related to b, say the same team or a tooling module, and b's owner agrees, b can add a qualified export, exports com.b.impl to a, which grants access only to a and keeps it hidden from everyone else.
Third, as a temporary workaround, especially for a third-party module, I can launch with --add-exports b/com.b.impl=a, and the same flag on javac if needed, but with a ticket to remove it, because it hides a fragile dependency. What I would avoid is an unqualified exports com.b.impl, because that makes an internal package public API for every module, permanently.
About this answer
- Last updated
- 12 November 2026
- 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 "A modular application starts with java.lang.IllegalAccessError: class X (in module a)...": https://scrivio-interview-library.netlify.app/java/modules/a0186b1c84/