nested exception is java.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool

本文分析了在使用MyEclipse整合Spring与Hibernate过程中遇到的关于配置文件读取错误的问题,并给出了具体的解决方案,包括缺失的commons-pool-1.3.jar包的引入及数据库驱动包的配置建议。

 

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} a:link, span.MsoHyperlink {color:blue; text-decoration:underline; text-underline:single;} a:visited, span.MsoHyperlinkFollowed {color:purple; text-decoration:underline; text-underline:single;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} -->

 

整合 Spring Hibernate Struts 好几次了,反反复复的 new Web Project ,一直发生这个错误,以为是导入的包有冲突 ... 现在才找到原因。
呵呵,搞得上个项目中没有使用 Spring

今天下午再继续整合时,才在网上找到症结所在。

本文小结参考:
http://www.blogjava.net/lusm/archive/2007/09/28/149363.html


每次在整合了 Spring Hibernate 后,就做了下小测试,发现总是读取 spring 的配置文件时错误。

MyEclipse6.0
控制台下的错误提示:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [applicationContext.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2357)
at java.lang.Class.getConstructor0(Class.java:2671)
at java.lang.Class.getDeclaredConstructor(Class.java:1953)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:54)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:752)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:717)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:386)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:249)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:155)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:246)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:291)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:122)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:66)
at org.ethip.fundsystem.test.Test.main(Test.java:8)

简要分析:

1.MyEclipse
在建立 SessionFactory 的时候,缺少一个包 commons-pool-1.3.jar
工程中少了 org/apache/commons/pool/impl/GenericObjectPool 所在的包
下载链接: http://mirrors.ibiblio.org/pub/mirrors/maven2/commons-pool/commons-pool/1.3/commons-pool-1.3.jar ( 或见附件 )

2.
如果加入该包之后还出现问题,请检查你的数据库驱动包是否配置正确 ( 由于是通过 MyEclipse 配置的,我这里没有出现这个错误 )

解决方法:
当然是在项目中导入 commons-pool-1.3.jar .

如果你的工程中已经有了这个包,请检查你的 tomcat jre 中是否有这个包,
有的话删除掉,以免因为重复而出现错误。

`java.lang.NoClassDefFoundError: org/apache/commons/io/output/UnsynchronizedByteArrayOutputStream` 是 Java 应用程序在运行时尝试加载某个类时,发现该类在编译时存在,但在运行时类路径中找不到其定义所引发的错误。此问题通常与依赖管理不当或版本不兼容有关。 ### 原因分析 1. **缺少 Commons-IO 依赖**:`UnsynchronizedByteArrayOutputStream` 是 Apache Commons IO 包中的一个类。如果项目未正确引入 `commons-io` 的依赖,或者构建过程中遗漏了该依赖,则会触发此类错误。 2. **依赖版本冲突**:某些库可能内部依赖于特定版本的 `commons-io`,而项目中使用的版本可能不兼容,导致类无法找到。 3. **依赖作用域配置错误**:Maven 或 Gradle 等构建工具中,若将依赖的作用域设置为 `provided` 或 `test`,则可能导致运行时无法加载该类。 4. **模块化系统(Java 9+)限制**:在使用模块化系统(JPMS)的 JDK 9 及以上版本中,某些类默认不再可访问,需通过 `--add-exports` 或 `--add-opens` 显式导出模块。 ### 解决方案 #### 1. 添加或更新 `commons-io` 依赖 确保 `pom.xml`(对于 Maven 项目)中包含以下依赖: ```xml <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.15.1</version> </dependency> ``` 对于 Gradle 项目,在 `build.gradle` 中添加: ```groovy implementation &#39;commons-io:commons-io:2.15.1&#39; ``` 升级到最新版本可以解决潜在的兼容性问题[^4]。 #### 2. 检查依赖作用域 确认依赖未被错误地标记为 `provided` 或 `test`,否则应将其更改为默认作用域(即 `compile` 或省略)以确保其在运行时可用。 #### 3. 排除冲突依赖 如果多个库引入了不同版本的 `commons-io`,可通过排除旧版本的方式统一使用新版本: ```xml <dependency> <groupId>some-other-library</groupId> <artifactId>some-artifact</artifactId> <exclusions> <exclusion> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> </exclusion> </exclusions> </dependency> ``` #### 4. 使用 `--add-exports`(仅限 JDK 9+) 如果问题是由于模块系统限制引起的,可在启动时添加如下 JVM 参数: ```bash --add-exports java.base/java.lang=ALL-UNNAMED ``` 根据实际需求调整包名和模块名。 #### 5. 清理并重新构建项目 执行以下命令清理并重新下载依赖: ```bash mvn clean install ``` 或对于 Gradle: ```bash ./gradlew clean build ``` #### 6. 验证 WAR 构建问题(如涉及 Web 项目) 若构建 WAR 文件时报错 `webxml attribute is required`,请在 `pom.xml` 中指定 `webXml` 属性: ```xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.3.0</version> <configuration> <webXml>src/main/webapp/WEB-INF/web.xml</webXml> </configuration> </plugin> ``` ### 示例代码:检查依赖是否正常加载 你可以编写一段简单的测试代码验证 `UnsynchronizedByteArrayOutputStream` 是否能正常加载: ```java import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream; public class TestCommonsIO { public static void main(String[] args) { try (UnsynchronizedByteArrayOutputStream out = new UnsynchronizedByteArrayOutputStream()) { out.write("Hello, World!".getBytes()); System.out.println(out.toString()); } catch (Exception e) { e.printStackTrace(); } } } ``` ###
评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值