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

    例如在 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
    
    • 1

    我的 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 allowsto opento, regardless of the module declaration.
    As a special case, if theisALL-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.

  • 相关阅读:
    23种设计模式详解
    CentOS 7升级gcc/G++版本
    7-154 打印杨辉三角(有注释)
    Unity减少发布打包文件的体积——获取精灵图片的信息限制它的大小
    基于Ubuntu20.04安装GoogleTest及其运行Sample
    阿里云oss丨NoSuchKey图片水印报错
    【2. 操作系统—中断、异常、系统调用】
    java 泛型
    ubuntu install docker
    深入剖析foreach底层原理以及并发修改异常
  • 原文地址:https://blog.csdn.net/m0_67391270/article/details/126040681