java.lang.NoClassDefFoundError: Lorg/apache/logging/log4j/Logger; 但神器存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39633796/
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
java.lang.NoClassDefFoundError: Lorg/apache/logging/log4j/Logger; but the artifact exists
提问by BackSlash
I'm using Tomcat to deploy a java webapp.
我正在使用 Tomcat 部署一个 java webapp。
I get a very long stacktrace, in short:
我得到一个很长的堆栈跟踪,简而言之:
GRAVE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/new-webapp]]
[...]
Caused by: java.lang.NoClassDefFoundError: Lorg/apache/logging/log4j/Logger;
at java.lang.Class.getDeclaredFields0(Native Method)
at java.lang.Class.privateGetDeclaredFields(Class.java:2509)
at java.lang.Class.getDeclaredFields(Class.java:1819)
at org.apache.catalina.util.Introspection.getDeclaredFields(Introspection.java:106)
at org.apache.catalina.startup.WebAnnotationSet.loadFieldsAnnotation(WebAnnotationSet.java:256)
at org.apache.catalina.startup.WebAnnotationSet.loadApplicationFilterAnnotations(WebAnnotationSet.java:105)
at org.apache.catalina.startup.WebAnnotationSet.loadApplicationAnnotations(WebAnnotationSet.java:64)
at org.apache.catalina.startup.ContextConfig.applicationAnnotationsConfig(ContextConfig.java:335)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:782)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:306)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:95)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5150)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
... 6 more
Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.Logger
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1305)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1139)
... 20 more
[...]
Now, the error is pretty clear. For some reason, the log4j bundle is not in the classpath.
现在,错误很明显了。出于某种原因,log4j 包不在类路径中。
The application is a maven webapp, and the pom.xml is like this:
应用是一个maven webapp,pom.xml是这样的:
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wb</groupId>
<artifactId>new-webapp</artifactId>
<packaging>war</packaging>
<version>0.0.1</version>
<properties>
<log4j.version>2.5</log4j.version>
</properties>
<dependencies>
[...]
<!-- Logging -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
[...]
</project>
If I go to the project properties, under Libraries->Maven Dependencies I see this:
如果我转到项目属性,在 Libraries->Maven Dependencies 下,我会看到:
Libraries were found, downloaded and they are in the classpath.
找到、下载了库,它们位于类路径中。
I also tried to open a java class inside my project and declare
我还尝试在我的项目中打开一个 java 类并声明
org.apache.logging.log4j.Logger Logger;
No errors, the Logger
interface is found.
没有报错,Logger
接口找到了。
What's going on here? Why does Tomcat fail to start even if libraries are in the classpath?
这里发生了什么?为什么即使库在类路径中,Tomcat 也无法启动?
Edit - this is the log4j configuration file:
编辑 - 这是 log4j 配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
回答by Little Santi
By the description you've made, I assume you are working with Eclipse.
根据您所做的描述,我假设您正在使用 Eclipse。
Well, you'd better go to Project properties
-> Deployment assembly
and ensure that the maven dependencies
entry is included.
好吧,你最好去Project properties
->Deployment assembly
并确保包含该maven dependencies
条目。
I've experimented often that this configuration gets missed whenever you execute Maven
-> Update Project
.
我经常试验,每当您执行Maven
->时都会错过此配置Update Project
。
回答by Little Santi
Ensure that the log4j libraries are not included already in the Tomcat runtime (nor the endorsed directories), because it could produce conflict about classloading policy.
确保 log4j 库未包含在 Tomcat 运行时(也不包含已认可的目录)中,因为它可能会产生有关类加载策略的冲突。
回答by Ucha
I'm working with Eclipse and I had same problem every time I made changes in my pom.xml. I don't know why but Eclipse delete the Maven Dependencies.
我正在使用 Eclipse,每次更改 pom.xml 时都会遇到同样的问题。我不知道为什么但是 Eclipse 删除了 Maven 依赖项。
Solution: rigth click on project, select Properties, choose Deployment Assemblyand verify in column "Source" a row called "Maven Dependencies". If it isn't there, click on Add..., Java Build Path Entriesand click on Maven Dependencies. Finally Apply and close.
解决方案:右键单击project,选择Properties,选择Deployment Assembly并在“Source”列中验证名为“Maven Dependencies”的行。如果它不存在,请单击Add...,Java Build Path Entries并单击Maven Dependencies。最后应用并关闭。