14

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.

3
  • 1
    You probably have a dependency crash, with one of your dependencies having a transitive dependency to log4j 1.x. Use mvn dependency:treeto find the culprit. Also, see stackoverflow.com/questions/26338387/… Commented Sep 3, 2015 at 6:53
  • 1
    You need to have the log4j jar in your class path. How are you executing your application? Commented Sep 3, 2015 at 6:53
  • Is there a way to do mvn dependency:tree in eclipse? I have not installed maven for commandline use, just using it in eclipse. I have added log4j-cire(2.3) and log4j-api(2.3) as dependencys in my pom.xml. I run it from the commandline with java -har myApplication.jar Commented Sep 3, 2015 at 6:57

3 Answers 3

21

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I have exactly the same problem and maven-shade-plugin is not working for me. I am using maven-shaden-plugin and once it creates the jar package I execute my app with java -jar myapp.jar and I continue receiving the same error. What am I doing wrong?
using the shade-plugin do I need to change the content of the <mainClass> ? Or is it another scope here?
@Garry " maven-shade-plugin " worked perfect. Thanks.
1

Please add jar files to class path in library, it worked for me

Comments

-2

Install the latest version of log4j (I have installed log4j-2.3.jar).

And follow the below steps:

  • Right click project -> Build path -> Libraries -> Add External Jars -> Include Log4j, Log4j core and Log4j api jars.

It worked for me

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.