Mohd Saif Usmani
Mohd Saif Usmani

Reputation: 81

How to fix "Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/core/io/support/SpringFactoriesLoader" in Spring Tool Suite

Spring Tool Suite editor is not giving any errors in my code but when I try to run as Spring Boot App, it gives me an exception of class def not found. All I am trying to do here is run the Spring Boot App which shows the spring boot logo in the console

I have checked all the dependencies in my xml file and they seem ok but not entirely sure.

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/core/io/support/SpringFactoriesLoader
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:428)
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:420)
    at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:268)
    at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:249)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
    at com.example.ToDoAppApplication.main(ToDoAppApplication.java:10)


Caused by: java.lang.ClassNotFoundException: org.springframework.core.io.support.SpringFactoriesLoader
    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)
    ... 7 more

Should just see that spring boot app is running successfully in the console without any errors.

Here is my xml 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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>ToDoApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ToDoApp</name>
    <description>ToDoApp for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.1.3.RELEASE</version><!--$NO-MVN-MAN-VER$-->
    </dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.1.4.RELEASE</version><!--$NO-MVN-MAN-VER$-->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.15</version><!--$NO-MVN-MAN-VER$-->
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-core -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <version>5.1.5.RELEASE</version><!--$NO-MVN-MAN-VER$-->
    </dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-web -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>5.1.5.RELEASE</version><!--$NO-MVN-MAN-VER$-->
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework.security/org.springframework.security.config -->
<!--    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>org.springframework.security.config</artifactId>
        <version>3.1.3.RELEASE</version>
    </dependency> -->


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Upvotes: 2

Views: 23446

Answers (2)

Andrewmb80
Andrewmb80

Reputation: 1

I don't have enough reputation to reply to Deinum... but managing my dependencies at the parent worked for me.

<parent>
        <!-- Your own application should inherit from spring-boot-starter-parent -->
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.6</version>
</parent>

I went to maven and just got the latest version and everything ran clean.

Upvotes: 0

M. Deinum
M. Deinum

Reputation: 124431

Your dependencies are a mess. YOu are mixing incompatible versions hence getting errors. Cleanup your dependencies in your pom.xml.

<dependencies>

    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

Remove the spring-core dependencies, which probably is the culprit in the first place. Don't use <version /> tags when including starters, those are managed through the parent. Instead of seperate Spring Security dependencies use the spring-boot-starter-security instead. You also had duplicate mysql-connector-java dependencies.

Upvotes: 3

Related Questions