当前位置:首页 » 《我的小黑屋》 » 正文

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

25 人参与  2024年03月26日 08:01  分类 : 《我的小黑屋》  评论

点击全文阅读


本篇文章介绍了 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)

出现此错误的原因有:

Apache Commons Logging jar 文件不正确或不存在。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 错误。


点击全文阅读


本文链接:http://zhangshiyu.com/post/85621.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1