在使用CGLIB创建Enhancer对象的时候,出现了以下错误信息:
"C:\Program Files\JDK\jdk-17.0.8\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2023.2.2\lib\idea_rt.jar=4816:C:\Program Files\JetBrains\IntelliJ IDEA 2023.2.2\bin" -Dfile.encoding=UTF-8 -classpath G:\Program\JAVA_WorkSpace\project1\Blogs\csdn\target\classes;C:\Users\xanadu\.m2\repository\cglib\cglib\3.2.4\cglib-3.2.4.jar;C:\Users\xanadu\.m2\repository\org\ow2\asm\asm\6.0_ALPHA\asm-6.0_ALPHA.jar;C:\Users\xanadu\.m2\repository\org\apache\ant\ant\1.9.6\ant-1.9.6.jar;C:\Users\xanadu\.m2\repository\org\apache\ant\ant-launcher\1.9.6\ant-launcher-1.9.6.jar com.huajieyu.code.y2024.sept.at0915b.cglib_proxy.ProxyTest
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.huajieyu.code.y2024.sept.at0915b.cglib_proxy.CGProxy.getProxy(CGProxy.java:17)
at com.huajieyu.code.y2024.sept.at0915b.cglib_proxy.ProxyTest.main(ProxyTest.java:13)
Caused by: java.lang.IllegalStateException: Unable to load cache item
at net.sf.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:79)
at net.sf.cglib.core.internal.LoadingCache.get(LoadingCache.java:34)
at net.sf.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:116)
at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:291)
at net.sf.cglib.core.KeyFactory$Generator.create(KeyFactory.java:221)
at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:174)
at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:153)
at net.sf.cglib.proxy.Enhancer.<clinit>(Enhancer.java:73)
... 2 more
Caused by: java.lang.ExceptionInInitializerError
at net.sf.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:243)
at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
at net.sf.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:329)
at net.sf.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:93)
at net.sf.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:91)
at net.sf.cglib.core.internal.LoadingCache$2.call(LoadingCache.java:54)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at net.sf.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:61)
... 9 more
Caused by: java.lang.reflect.InaccessibleObjectException: 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 @3d71d552
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)
at net.sf.cglib.core.ReflectUtils$1.run(ReflectUtils.java:54)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:318)
at net.sf.cglib.core.ReflectUtils.<clinit>(ReflectUtils.java:44)
... 17 more
Process finished with exit code 1
抓住几个重要的部分
【Caused by: java.lang.IllegalStateException: Unable to load cache item】
【Caused by: java.lang.ExceptionInInitializerError】
【Caused by: java.lang.reflect.InaccessibleObjectException: 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 @3d71d552】
可以看到其实是因为【module java.base does not “opens java.lang” to unnamed module @3d71d552】,直接翻译一下就是【模块 java.base 不会“打开 java.lang”到未命名的模块 @3d71d552】,所以需要在环境中添加以下配置(在vm options下添加即可),让程序支持java.base能打开java.lang到未命名模块。
--add-opens java.base/java.lang=ALL-UNNAMED
再次运行代码即可。