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=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 dodeep reflectionto access nonpublic members, then use the--add-opensruntime option.

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

If--illegal-access=deny, and you seeIllegalAccessExceptionorInaccessibleObjectExceptionmessages at runtime, you could use the--add-opensruntime option, basing the arguments upon the information shown in the exception message.

The syntax for--add-opensis:

–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>isALL-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 thejava.management/sun.managementpackage.

先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦

更多推荐

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