Announcement Announcement Module
Collapse
No announcement yet.
Spring Batch Maven Dependencies causing ClassNotFound exception on runtime Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Batch Maven Dependencies causing ClassNotFound exception on runtime

    Hi,

    I had a simple stand-alone Spring Integration application (basic AMQP and JDBC functionalities).

    However, I also want to add Spring Batch dependencies to my application. However, when I added the Spring Batch dependency in my pom.xml, I keep getting the following error when attempting to launch my app:

    Code:
    java.lang.NoClassDefFoundError: org/springframework/core/env/EnvironmentCapable
    	at java.lang.ClassLoader.defineClass1(Native Method)
    	at java.lang.ClassLoader.defineClassCond(Unknown Source)
    	at java.lang.ClassLoader.defineClass(Unknown Source)
    	at java.security.SecureClassLoader.defineClass(Unknown Source)
    	at java.net.URLClassLoader.defineClass(Unknown Source)
    	at java.net.URLClassLoader.access$000(Unknown Source)
    	at java.net.URLClassLoader$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.net.URLClassLoader.findClass(Unknown Source)
    	at java.lang.ClassLoader.loadClass(Unknown Source)
    	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    	at java.lang.ClassLoader.loadClass(Unknown Source)
    	at java.lang.ClassLoader.defineClass1(Native Method)
    	at java.lang.ClassLoader.defineClassCond(Unknown Source)
    	at java.lang.ClassLoader.defineClass(Unknown Source)
    	at java.security.SecureClassLoader.defineClass(Unknown Source)
    	at java.net.URLClassLoader.defineClass(Unknown Source)
    	at java.net.URLClassLoader.access$000(Unknown Source)
    	at java.net.URLClassLoader$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.net.URLClassLoader.findClass(Unknown Source)
    	at java.lang.ClassLoader.loadClass(Unknown Source)
    	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    	at java.lang.ClassLoader.loadClass(Unknown Source)
    	at java.lang.ClassLoader.defineClass1(Native Method)
    	at java.lang.ClassLoader.defineClassCond(Unknown Source)
    	at java.lang.ClassLoader.defineClass(Unknown Source)
    	at java.security.SecureClassLoader.defineClass(Unknown Source)
    	at java.net.URLClassLoader.defineClass(Unknown Source)
    	at java.net.URLClassLoader.access$000(Unknown Source)
    	at java.net.URLClassLoader$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.net.URLClassLoader.findClass(Unknown Source)
    	at java.lang.ClassLoader.loadClass(Unknown Source)
    	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    	at java.lang.ClassLoader.loadClass(Unknown Source)
    Caused by: java.lang.ClassNotFoundException: org.springframework.core.env.EnvironmentCapable
    	at java.net.URLClassLoader$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.net.URLClassLoader.findClass(Unknown Source)
    	at java.lang.ClassLoader.loadClass(Unknown Source)
    	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    	at java.lang.ClassLoader.loadClass(Unknown Source)
    	... 36 more
    The dependencies are shown below:
    Code:
     <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <spring.integration.version>2.2.0.RELEASE</spring.integration.version>
            <spring.batch.version>2.1.9.RELEASE</spring.batch.version>
        </properties>
    
    
            <!-- Spring Integration -->
            <dependency>
                <groupId>org.springframework.integration</groupId>
                <artifactId>spring-integration-core</artifactId>
                <version>${spring.integration.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.integration</groupId>
                <artifactId>spring-integration-amqp</artifactId>
                <version>${spring.integration.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.integration</groupId>
                <artifactId>spring-integration-stream</artifactId>
                <version>${spring.integration.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.integration</groupId>
                <artifactId>spring-integration-http</artifactId>
                <version>${spring.integration.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.integration</groupId>
                <artifactId>spring-integration-jdbc</artifactId>
                <version>${spring.integration.version}</version>
            </dependency>
    
            <!-- Spring Batch -->
            <dependency>
                <groupId>org.springframework.batch</groupId>
                <artifactId>spring-batch-core</artifactId>
                <version>${spring.batch.version}</version>
            </dependency>
    Upon removing the dependency, the exception is not thrown anymore. Has anyone else had similar issues in the past regarding this?

    Best regards

  • #2
    Adding spring-batch-core to your project causes Maven to downgrade spring-core and spring-beans to version 2.5.6. The remainder of your Spring dependencies are most likely using version 3.1.3.RELEASE.

    The fundamental problem here is the fact that spring batch 2.1.9.RELEASE depends on Spring 2.5.6 while Spring Integration 2.2.0.RELEASE depends on Spring 3.1.3.RELEASE. By default Maven resolves version conflicts with a nearest-wins strategy (http://maven.apache.org/guides/intro...mechanism.html).

    You can see what happens most clearly by running the following command on your project:

    [CODE]
    mvn dependency:tree -Dverbose
    [CODE]

    In the output you'll most likely find several lines that look somewhat like this:

    Code:
    [INFO] |  |  +- (org.springframework:spring-beans:jar:3.1.3.RELEASE:compile - omitted for conflict with 2.5.6)
    [INFO] |  |  \- (org.springframework:spring-core:jar:3.1.3.RELEASE:compile - omitted for conflict with 2.5.6)
    You can most likely fix the problem by adding a dependencyManagement-section to your project. You can then lock down all your Spring dependencies there to avoid this problem.

    Comment


    • #3
      Hi lothor,

      Thanks for your reply and informative explanation.

      I ended up amending my spring-batch dependency as follows, which seems to have solve the issue:
      Code:
        <!-- Spring Batch -->
              <dependency>
                  <groupId>org.springframework.batch</groupId>
                  <artifactId>spring-batch-core</artifactId>
                  <version>${spring.batch.version}</version>
                  <exclusions>
                      <exclusion>
                          <groupId>org.springframework</groupId>
                          <artifactId>spring-beans</artifactId>
                      </exclusion>
                      <exclusion>
                          <groupId>org.springframework</groupId>
                          <artifactId>spring-core</artifactId>
                      </exclusion>
                  </exclusions>
              </dependency>
      Best regards

      Comment

      Working...
      X