Spring Boot 错误:java.lang.NoClassDefFoundError:org/springframework/util/Assert
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49346778/
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
Spring Boot Error: java.lang.NoClassDefFoundError: org/springframework/util/Assert
提问by user182944
I am new to Spring Boot and following some video tutorials to get started. I created a Maven project, added a parent dependency of artifact id spring-boot-starter-parent
, added dependency for spring-boot-starter-web
and spring-boot-starter-test
. This is the Application.java
:
我是 Spring Boot 的新手,正在学习一些视频教程以开始使用。我创建了一个 Maven 项目,添加了 artifact id 的父依赖项spring-boot-starter-parent
,为spring-boot-starter-web
and添加了依赖项spring-boot-starter-test
。这是Application.java
:
@RestController
@EnableAutoConfiguration
public class Application {
@RequestMapping("/")
public String home() {
return "Hello World";
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Note:
笔记:
src/test/java
and src/test/resources
packages are empty. This is a Maven Project, I am using STS, performed "Maven -> Update Project..." and cleaned/build the entire project multiple times using STS IDE and I am getting the following error. Also, I tried to create a new project in STS and still I am getting the following error:
src/test/java
并且src/test/resources
包裹是空的。这是一个 Maven 项目,我正在使用 STS,执行“Maven -> 更新项目...”并使用 STS IDE 多次清理/构建整个项目,但出现以下错误。另外,我尝试在 STS 中创建一个新项目,但仍然出现以下错误:
I am getting this error when I run this as a Spring Boot App
:
当我将它作为以下运行时,我收到此错误Spring Boot App
:
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/util/Assert
at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:263)
at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:247)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1234)
at com.boot.Application.main(Application.java:18)
Caused by: java.lang.ClassNotFoundException: org.springframework.util.Assert
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)
... 5 more
Please help me in resolving this issue.
请帮我解决这个问题。
EDIT:
编辑:
This is my pom.xml:
这是我的 pom.xml:
<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.deloitte.boot</groupId>
<artifactId>boot-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot-test</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
This is my project directory stricture:
这是我的项目目录限制:
回答by arielduarte
I have fixed that issue like this, first I removed my folder .m2 and then I run mvn clean install into my directory spring boot project. that's all!
我已经解决了这个问题,首先我删除了我的文件夹 .m2,然后我将 mvn clean install 运行到我的目录 spring boot 项目中。就这样!
回答by mfaisalhyder
It is better to follow software principles while you are learning and making projects. Try to make project in a way that separation of concernis always achieved, that will make your code not just easy to understand, but to debug and fix too.
在学习和制作项目时,最好遵循软件原则。尝试以始终实现关注点分离的方式制作项目,这将使您的代码不仅易于理解,而且还易于调试和修复。
Change your application class like this:
像这样更改您的应用程序类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Now make another package controllerand make class in it for your testing
现在创建另一个包控制器并在其中创建类以进行测试
@RestController
//You can add this if you want or remove this class level mapping
@RequestMapping("/testApp")
public class TestController {
@RequestMapping("/")
public String home() {
return "Hello World";
}
}
For further help. please check this (official spring.io tutorial)simple example of Spring Boot App in STS. And another one is this very simple and straightforwardto get Spring Boot App up and running.
如需进一步帮助。请查看此(官方 spring.io 教程)STS 中 Spring Boot App 的简单示例。另一个是让 Spring Boot App 启动和运行非常简单和直接。
Edit: please add starter test in pom.xml as well.
编辑:也请在 pom.xml 中添加入门测试。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.5.10.RELEASE</version>
<scope>test</scope>
</dependency>
回答by Mike
Had the same issue with IntelliJ and a brand new project created from Spring Initializer. Saw this message when trying to compile manually:
IntelliJ 和从 Spring Initializer 创建的全新项目有同样的问题。尝试手动编译时看到此消息:
[ERROR] error reading /Users/mike/.m2/repository/org/springframework/spring-core/5.1.4.RELEASE/spring-core-5.1.4.RELEASE.jar; zip file is empty
Turns out the files downloaded from maven central were corrupt:
原来从 Maven 中心下载的文件已损坏:
ls -al /Users/mike/.m2/repository/org/springframework/spring-core/5.1.4.RELEASE/spring-core-5.1.4.RELEASE.jar
-rw-r--r-- 1 mike staff 0 Jan 16 20:55
/Users/mike/.m2/repository/org/springframework/spring-core/5.1.4.RELEASE/spring-core-5.1.4.RELEASE.jar
Removing the entire directory and rebuilding forced it to download a new copy of of the missing library:
删除整个目录并重建迫使它下载丢失库的新副本:
./mvnw compile
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/spring-core/5.1.4.RELEASE/spring-core-5.1.4.RELEASE.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/spring-core/5.1.4.RELEASE/spring-core-5.1.4.RELEASE.pom (3.6 kB at 7.6 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/spring-core/5.1.4.RELEASE/spring-core-5.1.4.RELEASE.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/spring-core/5.1.4.RELEASE/spring-core-5.1.4.RELEASE.jar (1.3 MB at 3.6 MB/s)
回答by Azarov
Had the same issue, after I installed STS (before that, everything in Eclipse was going ok). I removed the springframework folder that was inside the .m2 folder, then ran 'mvn clean install' in my project directory. After that I encountered an issue with my assertj-core-3.11.1.jar and mockito-core-2.23.0.jar (ZipFile invalid LOC header (bad signature)), so I also removed them and ran "mvn spring-boot:run".
有同样的问题,在我安装 STS 之后(在此之前,Eclipse 中的一切正常)。我删除了 .m2 文件夹中的 springframework 文件夹,然后在我的项目目录中运行“mvn clean install”。之后我遇到了我的 assertj-core-3.11.1.jar 和 mockito-core-2.23.0.jar 问题(ZipFile 无效 LOC 标头(错误签名)),所以我也删除了它们并运行“mvn spring-boot :跑”。
Now it works (but I can't really explain why).
现在它起作用了(但我无法真正解释原因)。