I am using log4j 2.3 in my java application. I added the dependency via maven.
When running the program in eclipse everything work fine, but when I package it with maven and try to run the jar I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache logging/log4j/LogManager
at main.myclass.<clinit>(myclass.java:11)
Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.LogManager
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)
... 1 more
Why is it not able to find the class while running it from a jar?
Adding log4j 1.2
did not work either. The program is running fine in eclipse so there should be no missing dependency.
lang. NoClassDefFoundError, which means the Class Loader file responsible for dynamically loading classes can not find the . class file. So to remove this error, you should set your classpath to the location where your Class Loader is present.
NoClassDefFoundError is a common error in Java that occurs if a ClassLoader cannot find a particular class in the classpath while trying to load it. The Exception in thread "main" suggests that this error has occurred in the main thread, the thread which is responsible for running the Java application.
Allows the configuration of log4j from an external file. See doConfigure(String, LoggerRepository) for the expected format. It is sometimes useful to see how log4j is reading configuration files.
When you are running your application jar from command line your dependent jar are not available at runtime. You need to include any of these two plugins to pom.xml so have your dependencies available at runtime.
Using: maven-shade-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.sonatype.haven.HavenCli</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
Using:maven-dependency-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
When you will execute the mvn package
it will generate uber jar / or copy the dependencies to outputDirectory. I will prefer maven-shade-plugin to generate one jar will all dependencies.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With