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

IT小君   2021-12-09T03:36:37

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

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

问题:

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

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

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

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

请注意以下事项:

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

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

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

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

点击广告,支持我们为你提供更好的服务
评论(3)
IT小君

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

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

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

2021-12-09T03:36:38   回复
IT小君

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

2021-12-09T03:36:38   回复
IT小君

如前所述:

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

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

这是一个Migrating问题。

把这些加给你 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>

然后,您可以使用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

指:

2021-12-09T03:36:38   回复