java module_error ai_generated true

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

Also available as: JSON · Markdown · 中文
85%Fix Rate
88%Confidence
1Evidence
2023-09-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
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.html

Workarounds

  1. 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`
  2. 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;`
  3. 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.

中文步骤

  1. 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`
  2. Update the module-info.java of your application to add 'requires' and 'exports' directives: `requires java.base; exports com.example.myapp to some.module;`
  3. 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:

  1. 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.

  2. 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.

  3. 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.