java.lang.IllegalAccessError: class X (in module Y) cannot access class Z (in module W) because module W does not export Z to module Y
ID: java/illegalaccesserror-module-does-not-export
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| Java 9 | active | — | — | — |
| Java 11 | active | — | — | — |
| Java 17 | active | — | — | — |
| Java 21 | active | — | — | — |
| OpenJDK 11.0.20 | active | — | — | — |
Root Cause
This error occurs in Java 9+ module system when a class from one module tries to reflectively access a class from another module that does not export the package, typically due to missing '--add-exports' or '--add-opens' JVM flags.
generic中文
在Java 9+模块系统中发生,当一个模块中的类尝试通过反射访问另一个模块中的类,而目标模块未导出该包时,通常由于缺少'--add-exports'或'--add-opens' JVM标志。
Official Documentation
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/IllegalAccessError.htmlWorkarounds
-
90% success Add JVM flags: `--add-opens java.base/java.lang=ALL-UNNAMED` to open the package to all unnamed modules. For example: `java --add-opens java.base/java.lang=ALL-UNNAMED -jar myapp.jar`
Add JVM flags: `--add-opens java.base/java.lang=ALL-UNNAMED` to open the package to all unnamed modules. For example: `java --add-opens java.base/java.lang=ALL-UNNAMED -jar myapp.jar`
-
85% success Update the module-info.java of your application to add 'requires' and 'exports' directives: `requires java.base; exports com.example.myapp to some.module;`
Update the module-info.java of your application to add 'requires' and 'exports' directives: `requires java.base; exports com.example.myapp to some.module;`
-
75% success As a temporary workaround, run the application on the classpath (remove module-info.java) to bypass module system restrictions entirely.
As a temporary workaround, run the application on the classpath (remove module-info.java) to bypass module system restrictions entirely.
中文步骤
Add JVM flags: `--add-opens java.base/java.lang=ALL-UNNAMED` to open the package to all unnamed modules. For example: `java --add-opens java.base/java.lang=ALL-UNNAMED -jar myapp.jar`
Update the module-info.java of your application to add 'requires' and 'exports' directives: `requires java.base; exports com.example.myapp to some.module;`
As a temporary workaround, run the application on the classpath (remove module-info.java) to bypass module system restrictions entirely.
Dead Ends
Common approaches that don't work:
-
Adding the dependency to pom.xml or build.gradle to include the missing module
80% fail
The error is not about missing dependencies; it's about module access control. Adding the dependency does not grant reflective access to internal packages.
-
Setting the classpath to include all jars without module declarations
70% fail
If the application runs as a named module (e.g., with module-info.java), classpath changes do not override module access restrictions.
-
Recompiling the library with public modifiers on the target class
90% fail
The error is about module exports, not Java access modifiers; even public classes in non-exported packages are inaccessible.