关于解决module java.base does not “opens java.lang” to unnamed module @333291e3的办法
1.抛出异常
在使用Dubbo和Zookeeper做一个分布式项目时,抛出以下异常:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.2023-07-20 17:00:42.979 ERROR 6920 --- [ main] o.s.boot.SpringApplication : Application run failedorg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ServiceBean:com.bjpowernode.api.service.PlatBaseInfoService:1.0': Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError
以下是抛出异常的导致因素:
Caused by: java.lang.ExceptionInInitializerError: nullat java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) ~[na:na]at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
Caused by: java.lang.IllegalStateException: Failed to create adaptive instance: java.lang.IllegalStateException: Can't create adaptive extension interfaceorg.apache.dubbo.rpc.Protocol, cause: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @333291e3at org.apache.dubbo.common.extension.ExtensionLoader.getAdaptiveExtension(ExtensionLoader.java:486) ~[dubbo-2.7.3.jar:2.7.3]at org.apache.dubbo.config.ServiceConfig.<clinit>(ServiceConfig.java:119) ~[dubbo-2.7.3.jar:2.7.3]... 24 common frames omitted
还有2个Caused by,就不列举了。
2.异常原因
导致这个异常发生的原因是使用了JDK17,
3.解决办法
方案一:将JDK版本改为1.8即可!!!
方案二:如果使用JDK17,可以在vm options 增加以下运行参数 。
–add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.lang.reflect=ALL-UNNAMED
找到 vm options选项的步骤:
在vm options选项中填入:
–add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.lang.reflect=ALL-UNNAMED
本人尝试过了,这种方法会抛出新的异常,待解决。(可能是vm options的参数不对)
来源地址:https://blog.csdn.net/ilikeavr/article/details/131835031