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.
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:
mvn clean compile antrun:run -Pclass-antrun
mvn clean package antrun:run -Pjar-antrun
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