# java.lang.IllegalAccessError：类X（在模块Y中）无法访问类Z（在模块W中），因为模块W未将Z导出到模块Y

- **ID:** `java/illegalaccesserror-module-does-not-export`
- **领域:** java
- **类别:** module_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

在Java 9+模块系统中发生，当一个模块中的类尝试通过反射访问另一个模块中的类，而目标模块未导出该包时，通常由于缺少'--add-exports'或'--add-opens' JVM标志。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Java 9 | active | — | — |
| Java 11 | active | — | — |
| Java 17 | active | — | — |
| Java 21 | active | — | — |
| OpenJDK 11.0.20 | active | — | — |

## 解决方案

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

## 无效尝试

- **Adding the dependency to pom.xml or build.gradle to include the missing module** — The error is not about missing dependencies; it's about module access control. Adding the dependency does not grant reflective access to internal packages. (80% 失败率)
- **Setting the classpath to include all jars without module declarations** — If the application runs as a named module (e.g., with module-info.java), classpath changes do not override module access restrictions. (70% 失败率)
- **Recompiling the library with public modifiers on the target class** — The error is about module exports, not Java access modifiers; even public classes in non-exported packages are inaccessible. (90% 失败率)
