【问题标题】:unexpected exception: java.lang.NoClassDefFoundError: org/apache/log4j/LogManager意外异常:java.lang.NoClassDefFoundError: org/apache/log4j/LogManager
【发布时间】:2014-10-13 10:58:20
【问题描述】:

我正在开发一个 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 的依赖 jars 也在 war/WEB-INF/lib 中。 log4j2-core、log4j-api 以及其他一堆(例如常见的 apache)

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

我在使用 JAR 时为什么会出现这个奇怪的错误?还要注意 NoClassDefFoundError,它不是更常见的 ClassNotFoundException。请看What causes and what are the differences between NoClassDefFoundError and ClassNotFoundException?

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

【问题讨论】:

  • 您能否提供构建脚本...您可能缺少记录消息所需的 Log4J 依赖项。并在 Eclipse 和 Ant 中检查您的 Java 运行时/SDK(应该相同)
  • 不,我没有遗漏依赖项。请再次阅读问题。
  • 抱歉,但从您对问题的描述中,我无法理解您到底在做什么以及问题出在哪里 - 因此到目前为止没有人回答您的问题。
  • 这个其他答案可能与一些有这个问题的人更相关。 stackoverflow.com/questions/32368758/…

标签: java eclipse gwt jar


【解决方案1】:

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

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

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

【讨论】:

  • 听起来很有希望,我错过了它说的是 log4j 而不是 log4j2!我可以添加 1.2(但想避免它)。如何找出引用 1.2 的 JAR?在其他非 GWT 应用程序中,我们的“lib”目录仅包含 log4j2-core.jar 和 log4j-api.jar,我没有得到这个异常吗?为什么我在 Eclipse 中使用 Project-reference 时没有看到这个问题,我肯定没有在任何地方使用 1.2。
  • 有人可以澄清一下吗? log4j.Manager 可能是一个 log4j 构造,但它可以在 log4j v2.x jar 文件中找到。 log4j 2.x 是 log4j2 结构,对吗?
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    如前所述:

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

    当您结合使用 log4j 1.2 和 log4j 2.x 时,可能会遇到这个问题。所以你必须在你的项目中添加 bridge 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
    

    指:

    【讨论】:

      猜你喜欢
      • 2018-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多