8

I am trying to write a simple RESTful service using Spring Boot. However, there is an error message I am not able to solve. I have been researching and it looks like it is a conflict between SpringBoot versions, however I am not sure about how can I get rid of it.

I have this SpringBootApp:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * REST Service application
 */
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Associated with this pom.xml:

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.websystique.springmvc</groupId>
    <artifactId>Spring4MVCHelloWorldRestServiceDemo</artifactId>
    <packaging>war</packaging>
    <version>1.0.0</version>
    <name>Spring4MVCHelloWorldRestServiceDemo Maven Webapp</name>

    <properties>
        <springframework.version>4.3.0.RELEASE</springframework.version>
        <jackson.library>2.7.5</jackson.library>
        <spring.batch.version>2.1.9.RELEASE</spring.batch.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.4.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version> <!-- Or whatever JUnit you're using. -->
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.library}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>${jackson.library}</version>
        </dependency>

        <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>


    </dependencies>


    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.2</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.4</version>
                    <configuration>
                        <warSourceDirectory>src/main/webapp</warSourceDirectory>
                        <warName>Spring4MVCHelloWorldRestServiceDemo</warName>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>

        <finalName>Spring4MVCHelloWorldRestServiceDemo</finalName>
    </build>
</project>

Looks fine for me, but I am getting this error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/core/env/ConfigurableEnvironment
    at com.application.Application.main(Application.java:13)
Caused by: java.lang.ClassNotFoundException: org.springframework.core.env.ConfigurableEnvironment
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more
stack man
  • 2,303
  • 9
  • 34
  • 54

8 Answers8

5

Recommended Approach

  1. Go to Spring boot Initialzr site and select web stack as a dependency as shown in below figure.
  2. As Spring Boot uses the concept of Opinionated dependencies and Bill of Materials, It will automatically pull the other dependencies and resolve your classNotFoundException issue. And in your case it misses out the Spring-core-4.2.4.RELEASE.jar dependency.
  3. Execute the Maven Goal as clean install spring-boot:run -e and you can find your maven dependencies in your IDE and below given is a sample POM file.

      <?xml version="1.0" encoding="UTF-8"?>
     <project xmlns="http://maven.apache.org/POM/4.0.0"              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0     http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
       <groupId>com.example</groupId>
      <artifactId>demo</artifactId>
     <version>0.0.1-SNAPSHOT</version>
     <packaging>war</packaging>
    
         <name>demo</name>
        <description>Demo project for Spring Boot</description>
    
         <parent>
           <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-parent</artifactId>
         <version>1.4.0.RELEASE</version>
         <relativePath/> <!-- lookup parent from repository -->
       </parent>
    
        <properties>
          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
           <project.reporting.outputEncoding>UTF-       8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        </properties>
    
     <dependencies>
         <dependency>
           <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
    
         <dependency>
              <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                 <scope>provided</scope>
          </dependency>
        <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
         </dependency>
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
          < /dependency>
        </dependencies>
    
      <build>
        <finalName>demo</finalName>
           <plugins>
             <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
             </plugin>
          </plugins>
        </build>
    

Praveen Kumar K S
  • 3,024
  • 1
  • 24
  • 31
3

Spring boot is running -

<spring.version>4.3.2.RELEASE</spring.version>

For -

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>1.4.0.RELEASE</version>
    </dependency>

While you are importing -

    <springframework.version>4.3.0.RELEASE</springframework.version>

You can confirm on the master pom -

https://github.com/spring-projects/spring-boot/blob/v1.4.0.RELEASE/spring-boot-dependencies/pom.xml

Can you update the spring version in your pom? Or deal with managing maven dependencies

farrellmr
  • 1,815
  • 2
  • 15
  • 26
  • 1
    Thanks. I added it and rebuild, but it says "Missing artifact org.springframework.boot:spring-boot-starter-web:jar:4.3.2.RELEASE" – stack man Aug 31 '16 at 09:53
  • Did you run clean? Im checking the pom imports - you could remove core from yours, just checking the others – farrellmr Aug 31 '16 at 10:01
  • You could either remove the web dependencies, or remove the version - then they will inherit from the bom – farrellmr Aug 31 '16 at 10:11
2

Delete the springframework folder in repository, then press Alt + F5 and update the project (force update snapshot).

C:\Users\xxxx\.m2\repository\org\springframework
sɐunıɔןɐqɐp
  • 3,332
  • 15
  • 36
  • 40
Lot Si
  • 21
  • 1
0

changed the boot-starter-parent version from 1.5.1.RELEASE to 1.4.0.RELEASE.

0
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.2.RELEASE</version>
  </parent>

Please Try this one for your parent

0

I had same problem with Spring boot 2.1.9.RELEASE

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

Added pom dependency eliminated this exception:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.2.0.RELEASE</version>
    </dependency>

EDIT - better approach.

Also clean up of maven repository with spring dependencies resolved problem for me, there were not need to add spring-core 5.2.0.RELEASE in pom. Seems that when I had many versions of spring in repo some problem occurs. Also before again resolve dependencies dont forget clean project.

rm -rf ~/.m2/repository/org/springframework/
Damian JK
  • 479
  • 1
  • 3
  • 9
0

For me changing all

<version>2.4.10</version>

to

<version>2.4.3</version>

in pom.xml file worked.

Pravin Desai
  • 527
  • 5
  • 18
-1

I changed the boot-starter-parent configuration which was runnig version 2.0.0 to 1.4.0 and I was able to run the java application and got rid of this error

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>  <!-- changed it to version 1.4.0 from 2.0.0-->
    <relativePath/> <!-- lookup parent from repository -->
</parent>
Eihab
  • 7
  • 4
  • But downgrading to an old version is hardly a sollution to the problem that lasts for long. Can't you find something that works with a later version of spring boot starter? – Alexander Mar 31 '18 at 11:47