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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39245732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:59:34  来源:igfitidea点击:

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

javaeclipsespringmavenspring-boot

提问by stack man

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.

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

I have this SpringBootApp:

我有这个 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:

与此 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>
        <Hymanson.library>2.7.5</Hymanson.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.Hymanson.core</groupId>
            <artifactId>Hymanson-databind</artifactId>
            <version>${Hymanson.library}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.Hymanson.dataformat</groupId>
            <artifactId>Hymanson-dataformat-xml</artifactId>
            <version>${Hymanson.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

采纳答案by Praveen Kumar K S

Recommended Approach

推荐方法

  1. Go to Spring boot Initialzrsite 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 -eand 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>
    
  1. 转到Spring boot Initialzr站点并选择 web stack 作为依赖项,如下所示数字
  2. 由于 Spring Boot 使用了Opinionated 依赖项和物料清单的概念,它会自动拉取其他依赖项并解决您的 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>
    

回答by farrellmr

Spring boot is running -

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>

While you are importing -

在您导入时 -

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

You can confirm on the master pom -

您可以在主 pom 上确认-

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

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

你能在你的 pom 中更新 spring 版本吗?或者处理管理maven依赖

回答by Eihab

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

我将运行版本 2.0.0 的 boot-starter-parent 配置更改为 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>

回答by Lot Si

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

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

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

回答by Ramya R.V

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

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

回答by user7336873

<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

请为你的父母试试这个

回答by Damian JK

I had same problem with Spring boot 2.1.9.RELEASE

我对 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:

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

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

还清理了具有 spring 依赖项的 maven 存储库,为我解决了问题,不需要在 pom.xml 中添加 spring-core 5.2.0.RELEASE。似乎当我在 repo 中有许多版本的 spring 时会出现一些问题。同样在再次解决依赖项之前不要忘记清理项目。

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