解决IntelliJ IDEA报错Failed to read candidate component class: file [ ]; nested exception is org.

解决IntelliJ IDEA报错Failed to read candidate component class: file [ ]; nested exception is org.springframework.core.NestedIOException

  笔者使用 IntelliJ IDEA 运行以前的一个 Spring 项目时,以前记得运行正常的代码,如今一直报错,百思不得其解。然后发现报错发生在一个普通、基础性的 Spring 方法。笔者当时报错时的运行环境如下:

笔者报错时的运行环境:

  • JDK 13.0.2

  • IntelliJ IDEA 2020.1.2 (Ultimate Edition)

  • Spring 5.1.9.RELEASE

  这个报错的信息如下:

Exception in thread "XXX" org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [XXX.class]; nested exception is org.springframework.core.NestedIOException: ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn't supported yet: file [XXX.class]; nested exception is java.lang.IllegalArgumentException: Unsupported class file major version 57
	at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.scanCandidateComponents(ClassPathScanningCandidateComponentProvider.java:454)
	at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:316)
	at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:275)
	at org.springframework.context.annotation.ComponentScanBeanDefinitionParser.parse(ComponentScanBeanDefinitionParser.java:90)
	at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:74)
	at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1391)
	at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1371)
	at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:179)
	at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:149)
	at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:96)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:513)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:393)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:224)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:195)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:257)
	at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:128)
	at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:94)
	at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:133)
	at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:636)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:521)
	at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:144)
	at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:85)
	at XXX(XXX:XXX)
Caused by: org.springframework.core.NestedIOException: ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn't supported yet: file [XXX.class]; nested exception is java.lang.IllegalArgumentException: Unsupported class file major version 57
	at org.springframework.core.type.classreading.SimpleMetadataReader.(SimpleMetadataReader.java:57)
	at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:103)
	at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:123)
	at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.scanCandidateComponents(ClassPathScanningCandidateComponentProvider.java:430)
	... 25 more
Caused by: java.lang.IllegalArgumentException: Unsupported class file major version 57
	at org.springframework.asm.ClassReader.(ClassReader.java:184)
	at org.springframework.asm.ClassReader.(ClassReader.java:166)
	at org.springframework.asm.ClassReader.(ClassReader.java:152)
	at org.springframework.asm.ClassReader.(ClassReader.java:273)
	at org.springframework.core.type.classreading.SimpleMetadataReader.(SimpleMetadataReader.java:54)
	... 28 more

  笔者在经过反复试验排错后,终于找到了根因。这是因为笔者报错时,使用的 Spring 依赖与 JDK 版本不兼容所导致的。因为笔者当初成功运行该项目时,使用的是低版本的 JDK,而现在运行该项目时,使用的是现在安装的 JDK。换句话说,相对于 Spring 的版本Spring 5.1.9.RELEASE,JDK 的版本JDK 13.0.2太高了。

  于是,笔者调高 Spring 的版本,将Spring 5.1.9.RELEASE改为Spring 5.2.9.RELEASE,结果运行之后,上面的报错就消失了。当然,也可以选择降低 JDK 的版本,不过笔者通常不喜欢这样做。

笔者成功运行时的运行环境:

  • JDK 13.0.2

  • IntelliJ IDEA 2020.1.2 (Ultimate Edition)

  • Spring 5.2.9.RELEASE

你可能感兴趣的:(疑难杂症,Java,java,spring,报错,intellij,idea)