java.lang.NoClassDefFoundError:org / springframework / core / env / ConfigurableEnvironment

时间:2016-08-31 09:27:08

标签: java eclipse spring maven spring-boot

我正在尝试使用Spring Boot编写一个简单的RESTful服务。但是,有一个错误消息,我无法解决。我一直在研究,看起来它是SpringBoot版本之间的冲突,但我不知道如何摆脱它。

我有这个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);
    }
}

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

对我来说很好,但我收到了这个错误:

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

7 个答案:

答案 0 :(得分:5)

推荐方法

  1. 转到Spring boot Initialzr网站,选择网络堆栈作为依赖关系,如下面figure所示。
  2. 由于Spring Boot使用 Opinionated依赖项和Bill of Materials 的概念,它将自动提取其他依赖项并解决classNotFoundException问题。在你的情况下,它错过了Spring-core-4.2.4.RELEASE.jar依赖。
  3. clean install spring-boot:run -e 执行Maven目标,您可以在IDE中找到您的maven依赖项,下面给出的是一个示例POM文件。

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

答案 1 :(得分:2)

Spring boot正在运行 -

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

导入时 -

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

您可以在主pom上确认 -

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

你能更新pom中的弹簧版吗?或者处理maven依赖关系

答案 2 :(得分:1)

删除存储库中的springframework文件夹,然后按Alt + F5并更新项目(强制更新快照)。

C:\Users\xxxx\.m2\repository\org\springframework

答案 3 :(得分:0)

我将boot-starter-parent配置(运行版本2.0.0)更改为1.4.0并且我能够运行java应用程序并摆脱此错误

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

答案 4 :(得分:0)

将boot-starter-parent版本从1.5.1.RELEASE更改为1.4.0.RELEASE。

答案 5 :(得分:0)

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

请为您的父母尝试这个

答案 6 :(得分:0)

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

添加的pom依赖项消除了此异常:

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

编辑-更好的方法。

对我来说,清理带有弹簧依赖项的maven存储库也解决了我的问题,不需要在pom中添加spring-core 5.2.0.RELEASE。似乎在回购中有多个spring版本时,会发生一些问题。同样,在再次解决依赖项之前,请不要忘记清理项目。

rm -rf ~/.m2/repository/org/springframework/