Spring Boot 应用启动时 java.lang.reflect.InaccessibleObjectException 问题的解决

Spring Boot 的应用启动的时候遇到下面的错误 java.lang.reflect.InaccessibleObjectException: Unable to make private native ... accessible。

Set com.sun.jndi.rmi.object.trustURLCodebase = false
java.lang.reflect.InaccessibleObjectException: Unable to make private native java.lang.reflect.Field[] java.lang.Class.getDeclaredFields0(boolean) accessible: module java.base does not "opens java.lang" to unnamed module @326de728
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
    at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:199)
    at java.base/java.lang.reflect.Method.setAccessible(Method.java:193)

错误的原因是因为 JVM 的模块 java.base 没有对未命名的模块开放 java.lang 这个包的深度反射 API 的调用权限。 具体来说,是没有开放 setAccessible(true) API。

这个问题在 JDK 8 以及以上的版本容易遇到。 解决的方法是在启动 Java 应用的时候, 加上参数指定开放特定的 Module/Package,使得 unnamed module 可以访问指定的 package 下面的深度反射 API。 如果有多个 Package 需要开放深度反射 API,那么可以指定多个 --add-opens 参数。

--add-opens java.base/java.lang=ALL-UNNAMED

例如在 Spring Boot 应用启动时, 加上多个 --add-opens 参数启动应用:

java -Dsun.misc.URLClassPath.disableJarChecking=true  --add-opens jdk.naming.rmi/com.sun.jndi.rmi.registry=ALL-UNNAMED --add-opens java.base/java.lang=ALL-UNNAMED  --add-opens java.base/sun.security.action=ALL-UNNAMED --add-opens java.base/sun.net=ALL-UNNAMED  -jar target/my-web-app.jar

我的 SpringBoot 是 2.6.7,代码编译的目标 JDK 是 JDK 11,运行时 JDK 是 JDK 17。

Oracle Java 的官方文档有关于这块的说明。

​​​​​​Java Platform, Standard Edition Oracle JDK 9 Migration Guide, Release 9


 

--add-opens

If you have to allow code on the class path to do deep reflection to access nonpublic members, then use the --add-opens runtime option.

Some libraries do deep reflection, meaning setAccessible(true), so they can access all members, including private ones. You can grant this access using the --add-opens option on the java command line. No warning messages are generated as a result of using this option.

If --illegal-access=deny, and you see IllegalAccessException or InaccessibleObjectException messages at runtime, you could use the --add-opens runtime option, basing the arguments upon the information shown in the exception message.

The syntax for --add-opens is:

--add-opens module/package=target-module(,target-module)*

This option allows <module> to open <package> to <target-module>, regardless of the module declaration.
As a special case, if the <target-module> is ALL-UNNAMED, then the source package is exported to all unnamed modules, whether they exist initially or are created later on. For example:

--add-opens java.management/sun.management=ALL-UNNAMED

This example allows all of the code on the class path to access nonpublic members of public types in the java.management/sun.management package.

猜你喜欢

转载自blog.csdn.net/davidullua/article/details/125190152