Reputation: 8067
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
:
@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:
I am getting this error when I run this as a 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:
<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:
Upvotes: 1
Views: 34348
Reputation: 5142
Had the same issue with IntelliJ and a brand new project created from Spring Initializer. Saw this message when trying to compile manually:
[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:
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)
Upvotes: 1
Reputation: 3
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".
Now it works (but I can't really explain why).
Upvotes: 0
Reputation: 536
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!
Upvotes: 5
Reputation: 2284
It is better to follow software principles while you are learning and making projects. Try to make project in a way that separation of concern is 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 controller and 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 straightforward to get Spring Boot App up and running.
Edit: please add starter test in pom.xml as well.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.5.10.RELEASE</version>
<scope>test</scope>
</dependency>
Upvotes: 1