[FIXED] Slf4j and Logback error: java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory

Issue

I’m working on a simple Java application where I’m not using any mayor framework, just maven for building and dependency management, Lombok to avoid boilerplate and sfl4j and Logback for logging duties. When I run the application directly trough IntelliJ IDE, it works perfect, but when I try to run the maven generated jar file from the command line, I’m getting the error java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory.

My maven pom looks as follows:

<dependencies>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-core</artifactId>
      <version>1.2.3</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.30</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.2.3</version>
    </dependency>
    ...

Any idea?

Solution

I think that your generated jar file doesn’t contain your dependencies.

To add dependencies in the generated jar file, I use maven-assembly-plugin maven plugin

<build>
<plugins>
  <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
      <archive>
        <manifest>
          <mainClass>**your main class**</mainClass>
        </manifest>
      </archive>
    </configuration>
  </plugin>
</plugins>
</build>

I hope it works for you

Answered By – Ahmed HENTETI

Answer Checked By – David Marino (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published