Reputation: 5449
Here's a simple Java code with SL4J built with maven. Following commands have no issues. All of them work well mvn compile mvn package mvn clean install mvn exec:java
But when I try any of the following
java -cp target/MaventestApp-1.0-SNAPSHOT.jar com.maven.helloworld.App
jar tf ./target/MaventestApp-1.0-SNAPSHOT.jar
I get the following error
exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at com.maven.helloworld.App.main(App.java:17)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
Here is the relevant part of my pom.xml
file:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<slf4jVersion>1.6.1</slf4jVersion>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4jVersion}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4jVersion}</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.maven.helloworld.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.maven.helloworld.App</mainClass>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
Code for this sample is here
Can someone help me understand maven and execution process better? Appreciate your time.
Upvotes: 0
Views: 181
Reputation: 2120
The problem is how you are building your JAR file (dependencies are no included in it). You could use maven-assembly-plugin instead (see the differences between assembly and jar plugin here).
Basically, replace your current build section by the following:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.maven.helloworld.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Then execute the generated jar-with-dependencies using the following command:
java -jar target/MaventestApp-1.0-SNAPSHOT-jar-with-dependencies.jar
See more about creating executable JARs here.
Upvotes: 1
Reputation: 443
There is the Exec Maven Plugin that should do the job. Something like
mvn exec:java -Dexec.mainClass=com.maven.helloworld.App
The actual problem is that you only add your jar to the classpath and since you do not have the sl4j-classes inside your own archive but declared them as external dependency they are simple missing (aka NoClassDefFoundError
).
The command above links all runtime dependencies to the classpath as configured in the pom.
Upvotes: 1