John Stone

1996-04-04

中国 河北

解决在JDK8中org.aspectj.apache.bcel.classfile.ClassFormatException

芦苇小白

Dec 29, 2020 12:58:31 PM 94

异常摘要

org.aspectj.apache.bcel.classfile.ClassFormatException: Invalid byte tag in constant pool: 18
    at org.aspectj.apache.bcel.classfile.Constant.readConstant(Constant.java:133)
    at org.aspectj.apache.bcel.classfile.ConstantPool.<init>(ConstantPool.java:45)
    at org.aspectj.apache.bcel.classfile.ClassParser.readConstantPool(ClassParser.java:186)
    at org.aspectj.apache.bcel.classfile.ClassParser.parse(ClassParser.java:131)
    at org.aspectj.apache.bcel.util.NonCachingClassLoaderRepository.loadJavaClass(NonCachingClassLoaderRepository.java:262)
    at org.aspectj.apache.bcel.util.NonCachingClassLoaderRepository.loadClass(NonCachingClassLoaderRepository.java:242)
    at org.aspectj.apache.bcel.util.NonCachingClassLoaderRepository.loadClass(NonCachingClassLoaderRepository.java:249)
    at org.aspectj.weaver.reflect.Java15AnnotationFinder.getAnnotations(Java15AnnotationFinder.java:202)
    at org.aspectj.weaver.reflect.ReflectionBasedResolvedMemberImpl.unpackAnnotations(ReflectionBasedResolvedMemberImpl.java:211)
    at org.aspectj.weaver.reflect.ReflectionBasedResolvedMemberImpl.hasAnnotation(ReflectionBasedResolvedMemberImpl.java:163)
    at org.aspectj.weaver.patterns.ExactAnnotationTypePattern.matches(ExactAnnotationTypePattern.java:109)
    at org.aspectj.weaver.patterns.ExactAnnotationTypePattern.matches(ExactAnnotationTypePattern.java:96)
    at org.aspectj.weaver.patterns.AnnotationPointcut.matchInternal(AnnotationPointcut.java:156)
    at org.aspectj.weaver.patterns.Pointcut.match(Pointcut.java:137)
    at org.aspectj.weaver.internal.tools.PointcutExpressionImpl.getShadowMatch(PointcutExpressionImpl.java:239)
    at org.aspectj.weaver.internal.tools.PointcutExpressionImpl.matchesExecution(PointcutExpressionImpl.java:105)
    at org.aspectj.weaver.internal.tools.PointcutExpressionImpl.matchesMethodExecution(PointcutExpressionImpl.java:96)
    at org.springframework.aop.aspectj.AspectJExpressionPointcut.getShadowMatch(AspectJExpressionPointcut.java:416)
    at org.springframework.aop.aspectj.AspectJExpressionPointcut.matches(AspectJExpressionPointcut.java:271)
    at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:210)
    at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:248)
    at org.springframework.aop.support.AopUtils.findAdvisorsThatCanApply(AopUtils.java:280)
    at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findAdvisorsThatCanApply(AbstractAdvisorAutoProxyCreator.java:118)
    at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findEligibleAdvisors(AbstractAdvisorAutoProxyCreator.java:88)
    at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(AbstractAdvisorAutoProxyCreator.java:69)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:346)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:298)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:422)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4842)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5303)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1407)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1397)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

触发场景

JDK1.8环境+Spring4+Mybatis3

异常分析

这个异常有点儿莫名其妙,我也是第一次遇见,一般这个时候我会看一下这个异常会引起的其他异常,或许可以找到原因,下面是该异常引起的一个Service实例化失败的异常报文

[org.springframework.web.context.support.XmlWebApplicationContext] - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'appUserServiceImpl' defined in file [F:\workspace_n\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\usermanager\WEB-INF\classes\com\aiyi\base\app\user\impl\AppUserServiceImpl.class]: Initialization of bean failed; nested exception is org.aspectj.apache.bcel.classfile.ClassFormatException: File: 'com.aiyi.base.app.user.impl.AppUserServiceImpl': Invalid byte tag in constant pool: 18
[org.springframework.beans.factory.support.DisposableBeanAdapter] - Invocation of destroy method 'close' failed on bean with name 'sqlSession': java.lang.UnsupportedOperationException: Manual close is not allowed over a Spring managed SqlSession
[org.springframework.web.context.ContextLoader] - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'appUserServiceImpl' defined in file [F:\workspace_n\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\usermanager\WEB-INF\classes\com\aiyi\base\app\user\impl\AppUserServiceImpl.class]: Initialization of bean failed; nested exception is org.aspectj.apache.bcel.classfile.ClassFormatException: File: 'com.aiyi.base.app.user.impl.AppUserServiceImpl': Invalid byte tag in constant pool: 18
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4842)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5303)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1407)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1397)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.aspectj.apache.bcel.classfile.ClassFormatException: File: 'com.aiyi.base.app.user.impl.AppUserServiceImpl': Invalid byte tag in constant pool: 18
    at org.aspectj.apache.bcel.classfile.ClassParser.readConstantPool(ClassParser.java:192)
    at org.aspectj.apache.bcel.classfile.ClassParser.parse(ClassParser.java:131)
    at org.aspectj.apache.bcel.util.NonCachingClassLoaderRepository.loadJavaClass(NonCachingClassLoaderRepository.java:262)
    at org.aspectj.apache.bcel.util.NonCachingClassLoaderRepository.loadClass(NonCachingClassLoaderRepository.java:242)
    at org.aspectj.apache.bcel.util.NonCachingClassLoaderRepository.loadClass(NonCachingClassLoaderRepository.java:249)
    at org.aspectj.weaver.reflect.Java15AnnotationFinder.getAnnotations(Java15AnnotationFinder.java:202)
    at org.aspectj.weaver.reflect.ReflectionBasedResolvedMemberImpl.unpackAnnotations(ReflectionBasedResolvedMemberImpl.java:211)
    at org.aspectj.weaver.reflect.ReflectionBasedResolvedMemberImpl.hasAnnotation(ReflectionBasedResolvedMemberImpl.java:163)
    at org.aspectj.weaver.patterns.ExactAnnotationTypePattern.matches(ExactAnnotationTypePattern.java:109)
    at org.aspectj.weaver.patterns.ExactAnnotationTypePattern.matches(ExactAnnotationTypePattern.java:96)
    at org.aspectj.weaver.patterns.AnnotationPointcut.matchInternal(AnnotationPointcut.java:156)
    at org.aspectj.weaver.patterns.Pointcut.match(Pointcut.java:137)
    at org.aspectj.weaver.internal.tools.PointcutExpressionImpl.getShadowMatch(PointcutExpressionImpl.java:239)
    at org.aspectj.weaver.internal.tools.PointcutExpressionImpl.matchesExecution(PointcutExpressionImpl.java:105)
    at org.aspectj.weaver.internal.tools.PointcutExpressionImpl.matchesMethodExecution(PointcutExpressionImpl.java:96)
    at org.springframework.aop.aspectj.AspectJExpressionPointcut.getShadowMatch(AspectJExpressionPointcut.java:416)
    at org.springframework.aop.aspectj.AspectJExpressionPointcut.matches(AspectJExpressionPointcut.java:271)
    at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:210)
    at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:248)
    at org.springframework.aop.support.AopUtils.findAdvisorsThatCanApply(AopUtils.java:280)
    at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findAdvisorsThatCanApply(AbstractAdvisorAutoProxyCreator.java:118)
    at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findEligibleAdvisors(AbstractAdvisorAutoProxyCreator.java:88)
    at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(AbstractAdvisorAutoProxyCreator.java:69)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:346)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:298)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:422)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
    ... 20 more

从这里可以看到Spring在实例化AppUserServiceImpl这个类的时候失败了,具体失败原因是aspectj.weaver这个包引起的,在这个异常中有一段儿有意思的话:

“Invalid byte tag in constant pool(说的是在反射这个Bean的时候遇到无效的字节)”。

那么我想我大概知道异常原因了,估计是因为我在这个类里面使用了JDK1.8的一些新的特性,而aspectj.weaver不支持这个特性导致Invalid byte tag in constant pool。

异常解决

既然知道病因了就该对症下药,我特意看了一下我的pom文件,aspectj weaver这个包我引用的是1.6版,我去Maven中央仓库看了一下,我了个乖乖,都更新到1.9X了,果然是版本太老了。于是修改pom.xml将aspectj weaver的版本改为1.8.9。

<!-- 旧版本,不支持JDK1.7以上的新特性 -->
<!-- <dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.6.11</version>
</dependency> -->

<!-- 该版本完美解决 -->
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.9</version>
</dependency>

再次运行,问题解决。

后来我又在网上查阅过这方面的资料,可惜的是国内的网络上目前并没有过多的介绍。找来找去在一篇博客上看到了类似的问题,当然并没有得到有效地解决,而是退而求次将JDK1.8换成了JDK7来避免了这个异常。

在这里有点儿小感慨,想想现在可能是因为用1.6和1.7版本JDK的人比较多吧。即使用了更新的版本,也不一定会使用它的新特性,所以这个异常出现的几率目前来说不大。不过随着IT的更新迭代,可能接下来的一段儿时间里,会有很多人面临这个问题,所以特此将这个异常的解决方案写上来分享该大家。

拖动滑块验证
验证通过 验证失败

全部评论