1

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:

enter image description here

11
  • Tried clean? gradlle or mvn ? ide based clean and re build? Commented Mar 18, 2018 at 10:24
  • I am using STS, performed "Maven -> Update Project..." and cleaned/build the entire project multiple times and I am getting the same error. Also, I tried to create a new project in STS and still I am getting the same error. Please help. Commented Mar 18, 2018 at 10:30
  • Can you try using @SpringBootApplication annotation on your Application Class. Commented Mar 18, 2018 at 10:32
  • try as Isank suggested Commented Mar 18, 2018 at 10:33
  • Also it’s a good practice to keep your configuration/application and controller classes separate. Commented Mar 18, 2018 at 10:33

4 Answers 4

5

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Worked for me, why is the question now.
1

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>

8 Comments

I tried this and got the same ClassNotFound error. Please suggest.
please post your pom.xml
posted above, please help
where is starter-test?
This is not really helpful. I have the code already like that and still the error.
|
1

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)

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.