log4j2 java.lang.NoClassDefFoundError: org/apache/logging/log4j/LogManager
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32368758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
log4j2 java.lang.NoClassDefFoundError: org/apache/logging/log4j/LogManager
提问by Pabi
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:
我在我的 java 应用程序中使用 log4j 2.3。我通过 Maven 添加了依赖项。
在eclipse中运行程序时一切正常,但是当我用maven打包并尝试运行jar时,我收到以下错误:
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?
为什么在从 jar 运行它时无法找到该类?
Adding log4j 1.2
did not work either. The program is running fine in eclipse so there should be no missing dependency.
添加log4j 1.2
也没有用。该程序在 eclipse 中运行良好,因此不应缺少依赖项。
采纳答案by Garry
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.
当您从命令行运行应用程序 jar 时,您的依赖 jar 在运行时不可用。您需要将这两个插件中的任何一个包含到 pom.xml 中,以便在运行时可以使用您的依赖项。
Using: maven-shade-plugin
使用: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
使用: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.
当您执行mvn package
它时,它会生成 uber jar / 或将依赖项复制到 outputDirectory。我更喜欢 maven-shade-plugin 生成一个 jar 将所有依赖项。
回答by user11800640
Install the latest version of log4j
(I have installed log4j-2.3.jar
).
安装最新版本log4j
(我已经安装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.
- 右键单击项目 -> 构建路径 -> 库 -> 添加外部 jar -> 包含 Log4j、Log4j 核心和 Log4j api jar。
It worked for me
它对我有用