提问者:小点点

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


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

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

问题:

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

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

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

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

注意以下几点:

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

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

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

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


共3个答案

匿名用户

org. apache.log4j.LogManager是来自log4j 1.2(不是log4j2)的类。

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

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

匿名用户

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

匿名用户

如前所述:

org. apache.log4j.LogManager是来自log4j 1.2(不是log4j2)的类。

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

这是一个迁移问题。

  • Log4j-从Log4j 1. x迁移-Apache Log4j 2

将这些添加到您的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依赖项:解决来查看没有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

指:

>

  • Log4j 1. x适配器-Log4j 1.2桥-Apache Log4j 1.x兼容性API

    Log4j Commons Logging Adaptor-Commons Logging Bridge-Apache Log4j Commons Logging Bridge