log4j2 java.lang.NoClassDefFoundError: org/apache/logging/log4j/LogManager

落花浮王杯 提交于 2019-12-03 06:45:47

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.

user11800640

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!