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

查看:3794
本文介绍了log4j2 java.lang.NoClassDefFoundError:org / apache / logging / log4j / LogManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的java应用程序中使用log4j 2.3。我通过maven添加了依赖项。

在eclipse中运行程序时一切正常,但是当我用maven打包并尝试运行jar时出现以下错误:

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

为什么无法找到班级从jar运行它?

Why is it not able to find the class while running it from a jar?

添加 log4j 1.2 也不起作用。该程序在eclipse中正常运行,因此不应缺少依赖。

Adding log4j 1.2 did not work either. The program is running fine in eclipse so there should be no missing dependency.

推荐答案

从命令行运行应用程序jar时您的依赖jar在运行时不可用。您需要将这两个插件中的任何一个包含到pom.xml中,以便在运行时使用您的依赖项。

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.

使用:maven-shade-plugin

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>

使用:maven-dependency-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>

当你执行 mvn包时将生成uber jar /或将依赖项复制到outputDirectory。我更喜欢使用maven-shade-plugin生成一个jar将所有依赖项。

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.

这篇关于log4j2 java.lang.NoClassDefFoundError:org / apache / logging / log4j / LogManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆