beni0888
beni0888

Reputation: 1244

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

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?

Upvotes: 1

Views: 2897

Answers (2)

jlaufer
jlaufer

Reputation: 177

For me adding these two plugins and it works with a simple $mvn clean install

    <plugins>
      <plugin>
        <!-- Build an executable JAR -->
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.2.0</version>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <classpathPrefix>lib/</classpathPrefix>
              <mainClass>Main</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>

      <!--     Used to package artifact including dependencies into an uber-jar-->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>1.6</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

Upvotes: 0

Ahmed HENTETI
Ahmed HENTETI

Reputation: 1128

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

Upvotes: 1

Related Questions