when running my program after building it like this mvn clean install
then running it with java -jar target/Sorting-1.0-SNAPSHOT.jar
. I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at xyz.skwar.sorting.Main.<clinit>(Main.java:7)
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
I researched it myself by reading this articel, it is not related since it has to do with a dependency. I read the following stack overflow posts:
- 1. how to fix "java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory" error when run java jar application
- 2. "How to fix: java.lang.NoClassDefFoundError: org.slf4j.LoggerFactory"
- 3. "java.lang.NoClassDefFoundError: org.slf4j.LoggerFactory"
- 4. Maven with dependency on "logback-classic" still gives "java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory" exception
- 5. Java: Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
Those all did not fix the problem since I am including the logging libary not just the api, I tried the solution from the 4th one but also did not work. The 5th one also is not relevant since I am not setting a scope.
When I remove the slf4j-api
it fails with, even though acording to the slf4j docs logback-classic provides slf4j-api:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
And I also made I minimal code sample which still has the problem:
package xyz.skwar.sorting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Main {
private static final Logger logger = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) {
logger.info("test");
}
}
And here is also the entire pom.xml 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>xyz.skwar.sorting</groupId>
<artifactId>Sorting</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>20</maven.compiler.source>
<maven.compiler.target>20</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.slf4j.version>2.0.9</org.slf4j.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.8.0</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.4.11</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
</dependencies>
<pluginRepositories>
<pluginRepository>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
</pluginRepository>
</pluginRepositories>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>xyz.skwar.sorting.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
CodePudding user response:
There are two steps to create a fat jar using manual maven configuration.
- Copy dependencies under
libs
directory (Missing in your pom) - Create executable jar and specify classpath as
libs
directory. (You are already doing this.)
So use maven-dependency-plugin
to copy the dependencies under libs
directory and then it should work fine.