26

Can someone tell me how to solve this issue?

I have narrowed down the problem to the pom.xml file. My project works but when I add the following dependency I get an error

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.3.2.RELEASE</version>
    </dependency>

The error stack is as follow

ERROR DispatcherServlet - Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [C:\workspace-sts\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\Webtest1\WEB-INF\classes\com\springweb\controller\SetHomePageController.class]; nested exception is java.lang.IncompatibleClassChangeError: class org.springframework.core.type.classreading.ClassMetadataReadingVisitor has interface org.springframework.asm.ClassVisitor as super class
    at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:290) ~[spring-context-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:242) ~[spring-context-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.context.annotation.ComponentScanBeanDefinitionParser.parse(ComponentScanBeanDefinitionParser.java:84) ~[spring-context-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:73) ~[spring-beans-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1438) ~[spring-beans-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1428) ~[spring-beans-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:185) ~[spring-beans-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:139) ~[spring-beans-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:108) ~[spring-beans-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:493) ~[spring-beans-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:390) ~[spring-beans-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334) ~[spring-beans-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302) ~[spring-beans-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174) ~[spring-beans-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209) ~[spring-beans-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180) ~[spring-beans-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125) ~[spring-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94) ~[spring-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130) ~[spring-context-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537) ~[spring-context-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451) ~[spring-context-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651) ~[spring-webmvc-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:599) ~[spring-webmvc-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665) ~[spring-webmvc-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:518) ~[spring-webmvc-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:459) ~[spring-webmvc-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136) [spring-webmvc-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at javax.servlet.GenericServlet.init(GenericServlet.java:160) [servlet-api.jar:3.0.FR]
    at org.apache.catalina.core.Standar

I can't understand what is wrong with the dependency, I need spring-data jpa dependency later when I'm going to add a dao that uses extends JpaRepository.

Thanks in advance.

cpu2007
  • 905
  • 4
  • 11
  • 24

10 Answers10

109

Another reason is use of Spring 3 with Java 8. Java 8 requires Spring 4.

(not specific to this question, but for those who are googling the error message)

Aleksandr Dubinsky
  • 22,436
  • 15
  • 82
  • 99
  • 10
    More Google keywords..... my specific error when trying to use JDK8, Spring 3, and a lambda expression was "failed to read candidate component class file" followed by an ArrayIndexOutOfBoundsException – Skylar Sutton Sep 04 '15 at 17:12
  • 1
    @skylarsutton I don't think Google pays much attention to comments. Best course of action may be to create an SO question with the exact error message and accepted correct answer. – Aleksandr Dubinsky Sep 05 '15 at 17:33
  • In my case I'm using Spring 3 version `3.0.5.Release` and JDK 1.8 version `1.8.0_131` and it works perfect, my problem comes when I migrate to Spring 3 version `3.2.1.RELEASE`. – Jorge Valdés Aug 30 '17 at 08:03
27

You most likely have conflicting Spring dependencies on your classpath. It's probably an old spring-asm JAR.

Run mvn dependency:tree -Dincludes=:spring*:: and check for conflicts. The best way to avoid these is to put a dependency on the spring-framework-bom in your project's dependencyManagement section, as follows

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>4.0.3.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

These versions are supported.

Emerson Farrugia
  • 11,153
  • 5
  • 43
  • 51
  • I'm not sure I understand. Do I have to add that dependency to the pom.xml? I did but it didn't work... How do I remove spring-asm from my project? – cpu2007 Apr 01 '14 at 13:47
  • That goes in the `` section, not the `` section. If you've identified the dependency, add an `` section to it. After Googling for the exclusion, this answer came up, which is extremely similar: http://stackoverflow.com/questions/16801639/spring-core-3-2-2-spring-security-3-1-4-java-lang-incompatibleclasschangeerro – Emerson Farrugia Apr 01 '14 at 22:18
  • Thanks!,using the exclusion fixed the problem. I know this is a bit unrelated but, I hope someone can help. Using rest,I'm getting the data from the db. When I use $http.get in the front end with the following path "/api/list", it doesn't work. If I use "/Webtest3/api/list" then it works(website3 is the name of the project,my homepage loads with this url localhost:8080/Webtest3/).How can I configure it so that it accepts /api/list and not Website3/api/list? Thank you – cpu2007 Apr 02 '14 at 08:50
  • You need to turn that into a separate question and give details about the technologies you're using, so the right people can look at it. If you've resolved this Maven issue and this answer helped you, please mark it as 'accepted' by clicking the check mark. This helps keep the focus on other SO questions which still don't have answers. – Emerson Farrugia Apr 02 '14 at 10:55
  • your answer explaining that you need to put exclusion tag is the part that solved my issue; there's no check mark next to it, how do I mark it as the right answer? – cpu2007 Apr 02 '14 at 13:20
  • I had a very different problem but the command "mvn dependency:tree -Dincludes=:spring*::" helped me figure out the conflicts. thanks a lot. This was very helpful. Can you help me understand this command. If this works to see the spring dependencoes, what if i want to see the hibernate dependencies? – ishanbakshi Jul 02 '15 at 05:38
  • 1
    @user3058470 run `mvn dependency:tree` without `-Dincludes` to see the full output. The `-Dincludes` flag is just a filter to help you find the dependency you're interested in in all that output. – Emerson Farrugia Jul 03 '15 at 07:34
6

I am using spring 3.2.3.RELEASE.When i changed the jdk.version 1.8 to 1.6 in maven compiler plug-in it started working

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
            <source>${jdk.version}</source>
            <target>${jdk.version}</target>
        </configuration>
</plugin>
Vineeth Bhaskaran
  • 2,161
  • 1
  • 29
  • 36
  • Telling your Java 1.8 compiler to target 1.7 works as well. See also this question/answer: https://stackoverflow.com/questions/22526695/java-1-8-asm-classreader-failed-to-parse-class-file-probably-due-to-a-new-java – Marvo Nov 07 '18 at 23:28
2

If anyone is still facing this error and is refering the getting started guide from spring.io, not using the defaut package and placing your class files under a new package did the trick for me.

testuserx
  • 226
  • 2
  • 13
  • this helped me. I have modules with base package name as `com.example.test1.service1` and the other module with `com.example.test2.service2` and the third module which is using `service1` and `service2` from the above modules and the main class of the third module is by default under `com.example.test3` which is causing the error so I placed it under `com.example` which worked for me like a charm! – Asad Shakeel May 13 '20 at 19:32
2

How to solve this problem?

Solution1:Change spring version to >= 3.2.16.

Solution2:If your spring versin is low than 3.2.16, dont use Annotation such as :@Service @Controller and so on. Use bean.xml define a bean, the bean.xml don't contians "<context:component-scan base-package=""/>". if you must use <context:component-scan base-package=""/> please put in other bean.xml.

Abdur Rahman
  • 1,420
  • 1
  • 21
  • 32
walter-hb
  • 31
  • 1
2

I had a problem like yours, I added dependency Spring core to maven and all works

Example:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.3.17</version>
</dependency>
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
skellfun
  • 21
  • 1
0

In my case I had dependencies that brought in the spring-asm dependency that conflicted with the latest Spring core. So in Dependency Hierarchy search for "asm" and exclude spring asm from all dependencies having it.

My case: 1)

<dependency>
    <groupId>org.restlet.jee</groupId>
    <artifactId>org.restlet.ext.fileupload</artifactId>
    <version>2.3.1</version>
        <exclusions>
                <exclusion>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-asm</artifactId>
               </exclusion>
        </exclusions>
</dependency>

2)

<dependency>
    <groupId>org.restlet.jee</groupId>
    <artifactId>org.restlet.ext.spring</artifactId>
    <version>2.1.2</version>
         <exclusions>
                  <exclusion>
                         <groupId>org.springframework</groupId>
                         <artifactId>spring-asm</artifactId>
                  </exclusion>
         </exclusions>
</dependency>
0

I had a similar error due to a corrupted class file -- somehow the CI build had a zero bytes class file in the jar (despite a non-zero inner class file being present, others ok) - one broken file out of ~100 classes. It might have been a disk space issue, but took a while to realise (since the class was in a dependent project jar, built by Gradle into a fat-jar for a Spring Boot project).

user136776
  • 606
  • 5
  • 5
0

When I tried to change the sequence of jars in the build path with <classpathentry kind="lib" path="/CoreTrainSim-Software-COTS/spring_jars/spring-core-5.1.7.RELEASE.jar"/>

it worked

0

Upgrading Java and Spring versions resolved my problem. I just added the following to pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.0.5</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>


<properties>
    <java.version>17</java.version>
</properties>
Ismoil Shokirov
  • 2,213
  • 2
  • 16
  • 32