解决 Java 错误 Java.Lang.NoClassDefFoundError: Org/Apache/Commons/Logging/LogFactory

本篇文章介绍了 Java 中的 java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory 错误。


解决java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory

使用 Apache Commons 和 Spring 时,注入依赖项时可能会出现错误 java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory。 该错误通常如下所示:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at org.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:119)
    at org.springframework.context.support.AbstractXmlApplicationContext.<init>(AbstractXmlApplicationContext.java:55)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:77)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:65)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:56)
    at com.client.StoryReader.main(StoryReader.java:15)

出现此错误的原因有:

  1. Apache Commons Logging jar 文件不正确或不存在。
  2. Commons Logging 未添加到依赖项中。

针对上述原因,解决此问题的方法如下:

添加正确的 Apache Commons 日志 jar

如果 Apache Commons Logging jar 没有添加到项目的构建路径中或者添加的文件不正确,那么它将抛出 java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory 错误。

请按照以下步骤解决此问题:

  • 从此处下载 Apache Commons Logging jar。
  • 将 jar 添加到项目的构建路径中。

上述两个步骤应该可以解决错误 java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory。 如果您使用的是Maven或Spring,则需要添加依赖项。

将 Commons 日志记录添加到依赖项

如果您使用 Maven 并且依赖项中缺少 Apache Commons-Logging,则会抛出 java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory 错误。 将以下依赖项添加到 pom.xml:

<dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
</dependency>

添加上述依赖即可解决Maven的问题。

在使用 Spring 应用程序的情况下,开发人员有时会排除 Commons Logging,但忘记插入另一个日志框架,因为应用程序需要日志框架:

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
      <exclusions>
          <exclusion>
          <groupId>commons-logging</groupId>
          <artifactId>commons-logging</artifactId>
          </exclusion>
      </exclusions>
</dependency>

上面的pom.xml是针对Spring应用程序的,我们可以看到,Commons-Logging被排除在外; 这就是它抛出 java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory 错误的原因。

为了解决这个问题,我们必须添加另一个日志框架; 大多数时候,开发人员使用 SLF4j 日志记录。 我们还需要使用桥来重定向 Spring 的日志记录。

将以下内容添加到 Spring 应用程序的 pom.xml 中:

   <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
      <exclusions>
          <exclusion>
          <groupId>commons-logging</groupId>
          <artifactId>commons-logging</artifactId>
          </exclusion>
      </exclusions>
   </dependency>

   <!-- Redirect the Spring logging -->
   <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>jcl-over-slf4j</artifactId>
      <version>${jcl.slf4j.version}</version>
   </dependency>

   <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>${logback.version}</version>
   </dependency>

对 Maven 和 Spring 执行上述过程将修复 java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory 错误。

  • 22
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迹忆客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值