3

I get the following after running a Jar (Maven project):

java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory

The error is caused in the declaration of the variable:

private static final Logger logger = LoggerFactory.getLogger("name.log");

This error is caused because I need to add to the project classPath a slf4j implementation (this is what I understood after hours of searching)

The solutions I have read Online suggest adding to the Pom a dependency and that it should solve it. Well, for some reason it does not work for me.

Since it is a Maven project, my pom.xml looks like this:

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.13</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.13</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.13</version>
    </dependency>

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.1.3</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>1.1.3</version>
    </dependency>

May be relevant: When I run the project in Netbeans (IDE), it logs perfectly; but once I build and try to run the jar it throws the exception mention above.

I appreciate the help.

Alessandroempire
  • 1,640
  • 4
  • 31
  • 54
  • 2
    Possible duplicate of [Maven dependency versioning and java.lang.NoClassDefFoundError](http://stackoverflow.com/questions/1539078/maven-dependency-versioning-and-java-lang-noclassdeffounderror) –  Jan 15 '16 at 02:36
  • all `java.lang.NoClassDefFoundError` are caused by the **exact same thing** the specified class is **not on the classpath**, how you fix it is so broad and so specific that it would be off topic. –  Jan 15 '16 at 02:37
  • @JarrodRoberson I dont understand your mentality. First, its not a duplicate of that post that you indicate. Second, all the previous solutions ever tried do not work, hence I am asking. – Alessandroempire Jan 15 '16 at 02:39
  • you have not read and tried each one of the hundreds of questions with this exact same error `java.lang.NoClassDefFoundError` on stackoverflow, if you have then good luck with that. –  Jan 15 '16 at 02:44
  • 3
    Your problem is because netbeans IDE will execute it ADDING the jars on the classpath execution command something like `java -cp lib1,lib2,lib3 yourpackage.YourClass` When you try to run it outside of the ide either you do the same or you build your project in maven adding all dependencies inside your jar with some lugin as maven-assembly-plugin It will add all dependencies class inside your jar which means you will be able to do `java yourpackage.YourClass` – Jorge Campos Jan 15 '16 at 02:51
  • @JorgeCampos Your recommendation worked. If you answer the question I will marked it as accepted. – Alessandroempire Jan 15 '16 at 12:52
  • @Alessandroempire No need! Just glad that I could help!! cheers!! :) – Jorge Campos Jan 16 '16 at 20:24

1 Answers1

-2

It's not related to this issue, but keep only one slf4 implementation, e.g. slf4j-simple.

Now about the missing LoggerFactory. When running outside an application server, the libraries must be provided, either through an explicit classpath or by embedded the required jar archives in the generated jar (a fatjar).

For the former option, it can be done on the command line or with the jar manifest.

Below is a pom that shows the use of the manifest and the generation of a fatjar:

  • without packaging, using the maven classpath:

mvn clean compile antrun:run -Pclass-antrun

  • packaging with a classpath inside the manifest:

mvn clean package antrun:run -Pjar-antrun

  • packaging a fatjar:

mvn clean package antrun:run -Pfatjar

<?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>dummy-slf4j</groupId>
    <artifactId>dummy-slf4j</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <main.class>dummy.App</main.class>

        <jdk.version>1.6</jdk.version>
        <project.encoding>UTF-8</project.encoding>

        <project.build.sourceEncoding>${project.encoding}</project.build.sourceEncoding>
        <project.reporting.outputEncoding>${project.encoding}</project.reporting.outputEncoding>

        <maven.compiler.source>${jdk.version}</maven.compiler.source>
        <maven.compiler.target>${jdk.version}</maven.compiler.target>
        <maven.compiler.compilerVersion>${jdk.version}</maven.compiler.compilerVersion>
        <maven.compiler.fork>true</maven.compiler.fork>
        <maven.compiler.verbose>true</maven.compiler.verbose>
        <maven.compiler.optimize>true</maven.compiler.optimize>
        <maven.compiler.debug>true</maven.compiler.debug>

        <maven.jar.plugin.version>2.6</maven.jar.plugin.version>
        <maven.assembly.plugin.version>2.6</maven.assembly.plugin.version>
        <maven.dependency.plugin.version>2.10</maven.dependency.plugin.version>
        <maven.antrun.plugin.version>1.8</maven.antrun.plugin.version>

    </properties>

    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.13</version>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>${maven.jar.plugin.version}</version>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>${main.class}</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>${maven.antrun.plugin.version}</version>
                </plugin>
                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>${maven.assembly.plugin.version}</version>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <archive>
                            <manifest>
                                <mainClass>${main.class}</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                    <executions>
                        <execution>
                            <id>make-assembly</id>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <version>${maven.dependency.plugin.version}</version>
                    <executions>
                        <execution>
                            <id>copy-dependencies</id>
                            <phase>package</phase>
                            <goals>
                                <goal>copy-dependencies</goal>
                            </goals>
                            <configuration>
                                <includeScope>runtime</includeScope>
                                <outputDirectory>${project.build.directory}/lib/</outputDirectory>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>

    </build>

    <profiles>
        <profile>
            <id>jar-antrun</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-dependency-plugin</artifactId>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-jar-plugin</artifactId>
                        <configuration>
                            <archive>
                                <manifest>
                                    <addClasspath>true</addClasspath>
                                    <classpathPrefix>lib/</classpathPrefix>
                                </manifest>
                            </archive>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <configuration>
                            <target>
                                <java fork="true"
                                    jar="${project.build.directory}/${project.build.finalName}.jar" />
                            </target>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>class-antrun</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <configuration>
                            <target>
                                <java fork="true" classname="${main.class}">
                                    <classpath refid="maven.compile.classpath" />
                                </java>
                            </target>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>fatjar</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-assembly-plugin</artifactId>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <configuration>
                            <target>
                                <java fork="true"
                                jar="${project.build.directory}/${project.build.finalName}-jar-with-dependencies.jar" />
                            </target>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

With the main app:

package dummy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class App {
    private static final Logger logger = LoggerFactory.getLogger(App.class);
    public static void main(String[] args) {
        logger.info("toto");
    }
}

UPDATE: add a profile for a fatjar

atao
  • 835
  • 6
  • 13