WARNING: An illegal reflective access operation has occurred
ID: java/illegal-reflective-access-warning
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| Java 9 | active | — | — | — |
| Java 11 | active | — | — | — |
| Java 17 | active | — | — | — |
| Java 21 | active | — | — | — |
Root Cause
Code is using reflection to access a member of a class in a module that does not export the package to the caller, which is disallowed by the Java module system (JPMS) since Java 9.
generic中文
代码使用反射访问某个模块中类的成员,但该模块未将包导出给调用者,自 Java 9 起,Java 模块系统(JPMS)禁止此操作。
Official Documentation
https://docs.oracle.com/en/java/javase/17/migrate/migration-guide.pdfWorkarounds
-
90% success Add JVM args to open the specific package: `--add-opens java.base/java.lang=ALL-UNNAMED` (replace package and module as needed).
Add JVM args to open the specific package: `--add-opens java.base/java.lang=ALL-UNNAMED` (replace package and module as needed).
-
85% success Update the library to a version that uses proper module exports or uses `ModuleLayer` API to access non-exported packages.
Update the library to a version that uses proper module exports or uses `ModuleLayer` API to access non-exported packages.
-
95% success If you control the library, add `exports` directive in `module-info.java` to export the package to the caller module.
If you control the library, add `exports` directive in `module-info.java` to export the package to the caller module.
中文步骤
添加 JVM 参数以打开特定包:`--add-opens java.base/java.lang=ALL-UNNAMED`(根据需要替换模块和包)。
将库更新到使用正确模块导出或使用 `ModuleLayer` API 访问非导出包的版本。
如果你控制库,在 `module-info.java` 中添加 `exports` 指令,将包导出给调用者模块。
Dead Ends
Common approaches that don't work:
-
Add `--illegal-access=permit` JVM flag to allow all reflective access.
95% fail
The `--illegal-access=permit` flag was removed in Java 17; it only works in Java 9-16. In Java 17+, it causes an error: 'Java.lang.IllegalArgumentException: IllegalAccessError'.
-
Set `AccessibleObject.setAccessible(true)` to bypass the warning.
80% fail
The warning is generated by the JVM's reflective access check; setAccessible does not suppress the warning or fix the underlying module access issue.
-
Ignore the warning; it's just a warning.
70% fail
In future Java versions, illegal reflective access will throw an `InaccessibleObjectException` at runtime, breaking the application.