意外异常:java.lang.NoClassDefFoundError:org/apache/log4j/LogManager

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26338387/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:16:37  来源:igfitidea点击:

unexpected exception: java.lang.NoClassDefFoundError: org/apache/log4j/LogManager

javaeclipsegwtjar

提问by Peter Andersson

I'm developing a GWT application. It's using RPC to collect information from an internal system. It does so by using a library jar, lets call it alpha.jar. We are using this jar in many application so it works fine, and btw its built with ANT, outside eclipse.

我正在开发一个 GWT 应用程序。它使用 RPC 从内部系统收集信息。它通过使用库 jar 来实现,我们称之为 alpha.jar。我们在许多应用程序中使用这个 jar,所以它工作正常,顺便说一句,它是用 ANT 构建的,在 eclipse 之外。

Some classes in alpha.jar references LOG4J2 and also lots of other external jars, so when we run an application we pass a classpath to all those, and everything works out fine. Please note that this is not a simple beginners problem. The alpha.jar is working as it should, including calls to Log4J.

alpha.jar 中的一些类引用 LOG4J2 以及许多其他外部 jar,因此当我们运行应用程序时,我们将类路径传递给所有这些类,并且一切正常。请注意,这不是一个简单的初学者问题。alpha.jar 正在正常工作,包括对 Log4J 的调用。

The problem:

问题:

In Eclipse, I have this GWT application project and also the Alpha.jar project (with source code of course). The server part needs to instatiate alpha objects and communicate to the alpha system.

在 Eclipse 中,我有这个 GWT 应用程序项目和 Alpha.jar 项目(当然还有源代码)。服务器部分需要设置 alpha 对象并与 alpha 系统通信。

When do this in GWT by adding a build-path-reference to the Alpha project, my GWT app runs fine.

通过向 Alpha 项目添加构建路径引用在 GWT 中执行此操作时,我的 GWT 应用程序运行良好。

When I instead of the project reference include (in war/WEB-INF/lib) the alpha.jar and runs the app, I get the error in the title the first time I instantiate a class from alpha.jar.

当我而不是项目引用包含(在 war/WEB-INF/lib 中)alpha.jar 并运行应用程序时,当我第一次从 alpha.jar 实例化一个类时,标题中出现错误。

There are no peculiarities in how the alpha.jar is built, so basically it should be the same thing as the project in eclipse, right?

alpha.jar 的构建方式没有什么特别之处,所以基本上应该和eclipse中的项目是一样的吧?

Note the following:

请注意以下事项:

*) The alpha.jar's dependent jars are also in war/WEB-INF/lib. log4j2-core, log4j-api as well as a bunch of others (apache common for example)

*) alpha.jar 的依赖 jar 也在 war/WEB-INF/lib 中。log4j2-core、log4j-api 以及其他一些(例如常见的 apache)

*) If I remove the alpha.jar (and the code that calls it) and instead just add code that called LOG4J2, that code also works fine!

*) 如果我删除 alpha.jar(以及调用它的代码),而只是添加调用 LOG4J2 的代码,那么该代码也可以正常工作!

How come I get this weird error when using the JAR?? Note also the NoClassDefFoundError, its not the more common ClassNotFoundException. Pls see What causes and what are the differences between NoClassDefFoundError and ClassNotFoundException?

使用 JAR 时为什么会出现这个奇怪的错误?还要注意 NoClassDefFoundError,它不是更常见的 ClassNotFoundException。请参阅NoClassDefFoundError 和 ClassNotFoundException 之间的原因和区别是什么?

If you need more info let me know.

如果您需要更多信息,请告诉我。

采纳答案by Steve C

org.apache.log4j.LogManageris a class from log4j 1.2 (not log4j2).

org.apache.log4j.LogManager是 log4j 1.2 中的一个类(不是 log4j2)。

Therefore, one of your web app jars must be referencing it. The culprit should be visible in the stack trace.

因此,您的 Web 应用程序 jar 之一必须引用它。罪魁祸首应该在堆栈跟踪中可见。

Depending upon your circumstances, you may want to just add a log4j 1.2 jar to the web app as the two versions are completely independent of each other.

根据您的情况,您可能只想向 Web 应用程序添加一个 log4j 1.2 jar,因为这两个版本彼此完全独立。

回答by albgorski

If you have use log4j 2 please do not forget to name your log4j2.xml instead of log4j.xml

如果您使用 log4j 2,请不要忘记将您的 log4 j2.xml命名为 log4j.xml

回答by Mark Simon

as refered before:

如前所述:

org.apache.log4j.LogManageris a class from log4j 1.2 (not log4j2).

org.apache.log4j.LogManager是 log4j 1.2 中的一个类(不是 log4j2)。

this problem meets when you combine use log4j 1.2 and log4j 2.x, maybe. so you have to add bridge api to you project.

当您结合使用 log4j 1.2 和 log4j 2.x 时,可能会遇到此问题。所以你必须在你的项目中添加桥接 API。

this is a Migratingproblem.

这是一个Migrating问题。

add those to you pom.xml

把这些加给你 pom.xml

        <log4j2.version>2.7</log4j2.version>
        <disruptor.version>3.3.6</disruptor.version>

        <!--log4j2 dependencies -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${log4j2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${log4j2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-1.2-api</artifactId>
            <version>${log4j2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>${log4j2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-jcl</artifactId>
            <version>${log4j2.version}</version>
        </dependency>
        <dependency>
            <groupId>com.lmax</groupId>
            <artifactId>disruptor</artifactId>
            <version>${disruptor.version}</version>
        </dependency>

then, you could use mvn dependency:resolveto see no log4j 1.2

然后,您可以使用mvn dependency:resolve看不到 log4j 1.2

[INFO] The following files have been resolved:
[INFO]    org.springframework.data:spring-data-redis:jar:1.7.2.RELEASE:compile
[INFO]    org.apache.logging.log4j:log4j-api:jar:2.7:compile
[INFO]    org.apache.logging.log4j:log4j-slf4j-impl:jar:2.7:compile
[INFO]    com.lmax:disruptor:jar:3.3.6:compile
[INFO]    org.apache.logging.log4j:log4j-1.2-api:jar:2.7:compile
[INFO]    javax.mail:mail:jar:1.4.5:compile
[INFO]    org.springframework:spring-tx:jar:4.3.1.RELEASE:compile
[INFO]    org.apache.logging.log4j:log4j-core:jar:2.7:compile
[INFO]    org.apache.logging.log4j:log4j-jcl:jar:2.7:compile
[INFO]    javax.activation:activation:jar:1.1:compile
[INFO]    org.springframework:spring-beans:jar:4.3.1.RELEASE:compile
[INFO]    org.springframework:spring-web:jar:4.3.1.RELEASE:compile
[INFO]    org.springframework:spring-webmvc:jar:4.3.1.RELEASE:compile
[INFO]    org.springframework:spring-oxm:jar:4.2.6.RELEASE:compile
[INFO]    org.springframework:spring-jdbc:jar:4.3.1.RELEASE:compile
[INFO]    com.alibaba:fastjson:jar:1.2.4:compile
[INFO]    mysql:mysql-connector-java:jar:5.1.21:compile
[INFO]    org.apache.tomcat:tomcat-servlet-api:jar:7.0.54:provided
[INFO]    org.slf4j:slf4j-api:jar:1.7.21:compile
[INFO]    org.springframework:spring-context-support:jar:4.3.1.RELEASE:compile
[INFO]    commons-beanutils:commons-beanutils:jar:1.8.3:compile
[INFO]    org.springframework:spring-context:jar:4.3.1.RELEASE:compile
[INFO]    org.hamcrest:hamcrest-core:jar:1.3:test
[INFO]    redis.clients:jedis:jar:2.8.1:compile
[INFO]    org.springframework:spring-expression:jar:4.3.1.RELEASE:compile
[INFO]    org.springframework.data:spring-data-commons:jar:1.12.2.RELEASE:compile
[INFO]    org.springframework.data:spring-data-keyvalue:jar:1.1.2.RELEASE:compile
[INFO]    junit:junit:jar:4.12:test
[INFO]    org.springframework:spring-core:jar:4.3.1.RELEASE:compile
[INFO]    commons-logging:commons-logging:jar:1.2:compile
[INFO]    org.springframework:spring-aop:jar:4.3.1.RELEASE:compile
[INFO]    org.apache.commons:commons-pool2:jar:2.4.2:compile
[INFO]    org.slf4j:jcl-over-slf4j:jar:1.7.21:runtime

refers:

指: