javaspringspring-bootmaven

Packaged SpringBoot jar is missing the slf4j libraries, other ways of building work


I have a Maven SpringBoot application with source language Java 21. I think there is something wrong with adding my libraries in the classpath of my generated jar file. When I execute mvn package or mvn compile or start the SpringBootApplication in IntelliJ, everything seems to be fine. No issues, no dependency problems.

However when I execute a mvn package and afterwards I execute the java executable by running java -jar project-local-SNAPSHOT.jar I get following stack trace.

Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
        at com.emea.project.ProjectApplication.<clinit>(ProjectApplication.java:13)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)

This links to the @Slf4J annotation in my ProjectApplication class, which is the following logic:

package com.emea.project;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * Application starting point.
 */
@Slf4j
@EnableAsync
@EnableScheduling
@SpringBootApplication
@ComponentScan("com.emea.reporting")
public class ProjectApplication {
  
  public static void main(String[] args) {
    SpringApplication.run(ProjectApplication.class, args);
  }
}

To add additional information, I also add my pom file:

<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/maven-v4_0_0.xsd">

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.5.3</version>
        <relativePath/>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.emea</groupId>
    <artifactId>project</artifactId>
    <version>${revision}</version>

    <properties>
        <!-- Project properties -->
        <revision>local-SNAPSHOT</revision>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- Dependency versions -->
        <springboot-version>3.5.3</springboot-version>
        <lombok-version>1.18.38</lombok-version>
        <springjdbc-version>6.2.9</springjdbc-version>
        <jib.build-goal>build</jib.build-goal>
        <jib.image-provider>registry</jib.image-provider>
        <exec.mainClass>com.emea.project.ProjectApplication</exec.mainClass>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.google.code.findbugs</groupId>
            <artifactId>annotations</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.google.code.findbugs</groupId>
            <artifactId>jsr305</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.9.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents.client5</groupId>
            <artifactId>httpclient5</artifactId>
            <version>5.5</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.5.3</version>
            </plugin>
            <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <annotationProcessorPaths>
                            <path>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                                <version>${lombok-version}</version>
                            </path>
                        </annotationProcessorPaths>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.3.0</version>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>${exec.mainClass}</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
            </plugins>
    </build>
</project>

Solution

  • start your spring boot project from here:

    https://start.spring.io/

    enter image description here

    get project.zip

    unzip the project.zip

    you can find: pom.xml

    怌

    <?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 https://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>3.5.4</version>
            <relativePath/>
        </parent>
        <groupId>com.emea</groupId>
        <artifactId>project</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>project</name>
        <description>Demo project for Spring Boot</description>
        <properties>
            <java.version>21</java.version>
        </properties>
        <dependencies>
            <dependency>            <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <annotationProcessorPaths>
                            <path>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                            </path>
                        </annotationProcessorPaths>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <excludes>
                            <exclude>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                            </exclude>
                        </excludes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    (1) Use the spring-boot-maven-plugin to build the Spring Boot application JAR. Do not use the maven-jar-plugin.

    (2) In the section of your pom.xml, do not manually specify the versions of dependencies that are already managed by Spring Boot (e.g., 3.5.3, 1.18.38, 6.2.9).

    (3) You can use the mvn dependency:tree command to identify the libraries and versions that are already included by Spring Boot.

    package and run:

    package

    mvn clean package
    

    run

    java -jar target/project-0.0.1-SNAPSHOT.jar
    

    then everything is ok!

    2025-08-06T23:52:06.643+08:00  INFO 17710 --- [project] [           main] com.emea.project.ProjectApplication      : Started ProjectApplication in 1.387 seconds (process running for 1.986)
    

    To reproduce your issue, simply modify the pom.xml as follows:

    add

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.3.0</version>
        <configuration>
            <archive>
                <manifest>
                    <mainClass>${exec.mainClass}</mainClass>
                </manifest>
            </archive>
        </configuration>
    </plugin>
    

    remove

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <excludes>
                <exclude>
                    <groupId>org.projectlombok</groupId>
                    <artifactId>lombok</artifactId>
                </exclude>
            </excludes>
        </configuration>
    </plugin>
    

    add

    <exec.mainClass>com.emea.project.ProjectApplication</exec.mainClass>
    

    into <properties>

    modify ProjectApplication.java like yours.

    package and run:

    package

    mvn clean package
    

    run

    java -jar target/project-0.0.1-SNAPSHOT.jar
    

    then get the same error:

    $ java -jar target/project-0.0.1-SNAPSHOT.jar
    Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
        at com.emea.project.ProjectApplication.<clinit>(ProjectApplication.java:11)
    Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
        ... 1 more
    

    how to fix ?