4

I'm having troubles configuring slf4j with Spring. I'm using maven, and I can package a .jar, but when I run it, it gives the following exception:

Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
        at logger.Log.<clinit>(Log.java:8)
        at app.App.main(App.java:29)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 2 more

Here's how I'm using slf4j:

public class Log implements ILog {

    private static final Logger LOGGER = LoggerFactory.getLogger(Log.class);

    @Override
    public void reportMessage(String message) {
        LOGGER.info(message);
    }

    @Override
    public void reportWarningMessage(String message) {
        LOGGER.warn(message);
    }

    @Override
    public void reportErrorMessage(String message) {
        LOGGER.error(message);
    }
}

And here's the pom.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>

    <groupId>pt.isel.ps</groupId>
    <artifactId>neat</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.19</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-jdk14</artifactId>
            <version>1.7.19</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.3.0.RELEASE</version>
                <configuration>
                    <mainClass>app.App</mainClass>
                    <layout>ZIP</layout>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

If anyone could help me I would be very thankful.

codehero
  • 499
  • 3
  • 15
  • 1
    Possible duplicate of [java.lang.NoClassDefFoundError: org.slf4j.LoggerFactory](http://stackoverflow.com/questions/12926899/java-lang-noclassdeffounderror-org-slf4j-loggerfactory) – Software Engineer Sep 23 '16 at 17:16
  • I've tried what was suggested in the answer that you referred, but still getting that error... – codehero Sep 23 '16 at 17:40

1 Answers1

0

Obviously, org.slf4j.LoggerFactory class can't be found under your classpath.

  1. Try to pull the dependencies again, from command-line and from your project folder: dependency:copy-dependencies
  2. Try to sync your eclipse IDE by refreshing the project, and/or by m2e eclipse plugin by update dependencies.
  3. On command-line & from your project folder try to compile again using: mvn clean compile
  4. If that doesn't work, look for the unfound class under Maven dependencies in your project, (you should be able to find).
  5. Make sure your Maven dependencies are a part of your build configuration.
  6. Try to compile again: mvn clean compile

If you have the requested dependency in your Local Maven Repository, and it's included in your project, and your build configuration includes the Maven dependencies to be included in your output classpath folder. Then it should work.

Moshe Arad
  • 3,587
  • 4
  • 18
  • 33