The following error was encountered when the Spring Boot application was started:

1
2
3
4
5
6
7
8
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)

The reason for the error is that the JVM’s java.base module does not open the java.lang package deep reflection API call permissions for unnamed modules.

Specifically, the setAccessible(true) API is not open.

This problem is easily encountered in JDK 8 and above. The workaround is to start the Java application with a parameter specifying that a specific module/package is open, so that the unnamed module can access the deep reflection API under the specified package.

If there are multiple Packages that need to open the deep reflection API, then you can specify multiple --add-opens arguments.

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

For example, when starting a Spring Boot application from the command line, you can add multiple --add-opens arguments.

1
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

My SpringBoot version is 2.6.7, the code is compiled with JDK version 11, and the runtime JDK version is 17.

Oracle Java’s official documentation has instructions for this.

Reference https://blog.csdn.net/davidullua/article/details/125190152