poi4.0.0读取excel文件时报java.lang.NoClassDefFoundError: org/apache/commons/compress/archivers/zip/ZipFile

  • 最近想用poi写个处理excel的工具,看了一下poi的官网,出了个4.0.0的版本,于是想尝尝鲜,下载了一把poi4.0.0的bin。下载的是下面的文件:

poi4.0.0读取excel文件时报java.lang.NoClassDefFoundError: org/apache/commons/compress/archivers/zip/ZipFile_第1张图片

  •  下载完,将压缩包里所有的jar包导入到工程中(当然,如果只需要处理excel xlsx文件的话,只需要poi,poi-ooxml, poi-ooxml-schemas,xmlbeans,commons-collections这几个jar包就可以了。)

poi4.0.0读取excel文件时报java.lang.NoClassDefFoundError: org/apache/commons/compress/archivers/zip/ZipFile_第2张图片

  • 运行写好的代码,选择文件,读取excel文件时报了下面的错误:
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/commons/compress/archivers/zip/ZipFile
	at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:272)
	at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:181)
	at org.apache.poi.xssf.usermodel.XSSFWorkbook.(XSSFWorkbook.java:323)
	at cn.redsoft.com.sheetsort.ExcelSheetSort.excuteActionPerformed(ExcelSheetSort.java:169)
	at cn.redsoft.com.sheetsort.ExcelSheetSort.access$100(ExcelSheetSort.java:25)
	at cn.redsoft.com.sheetsort.ExcelSheetSort$2.actionPerformed(ExcelSheetSort.java:78)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
	at java.awt.Component.processMouseEvent(Component.java:6539)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
	at java.awt.Component.processEvent(Component.java:6304)
	at java.awt.Container.processEvent(Container.java:2239)
	at java.awt.Component.dispatchEventImpl(Component.java:4889)
	at java.awt.Container.dispatchEventImpl(Container.java:2297)
	at java.awt.Component.dispatchEvent(Component.java:4711)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
	at java.awt.Container.dispatchEventImpl(Container.java:2283)
	at java.awt.Window.dispatchEventImpl(Window.java:2746)
	at java.awt.Component.dispatchEvent(Component.java:4711)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
	at java.awt.EventQueue.access$500(EventQueue.java:97)
	at java.awt.EventQueue$3.run(EventQueue.java:709)
	at java.awt.EventQueue$3.run(EventQueue.java:703)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
	at java.awt.EventQueue$4.run(EventQueue.java:733)
	at java.awt.EventQueue$4.run(EventQueue.java:731)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.compress.archivers.zip.ZipFile
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	... 42 more
  • 这个错误意思是在运行时找不到org/apache/commons/compress这个package中的类,由此推断,应该是少了什么jar包,于是我去看了一下maven的网站,发现还有这么一层依赖关系。。

poi4.0.0读取excel文件时报java.lang.NoClassDefFoundError: org/apache/commons/compress/archivers/zip/ZipFile_第3张图片

  • 于是就取网上搜了一下这个jar包的官网,也是Apache的项目,是一个解压缩的项目。下载这个1.18版本的文件。poi4.0.0读取excel文件时报java.lang.NoClassDefFoundError: org/apache/commons/compress/archivers/zip/ZipFile_第4张图片
  • 下载完这个jar包导入到我的工程中,进行编译,运行程序,OK,解决了。
  • 所以如果不是通过maven来构筑的poi的项目,要从官网下载bin文件的朋友,就要注意了,官网的bin文件中少了一个commons-compress-1.18.jar,既然其他的commons-包都放进去了,怎么就少了一个compress呢。。。

你可能感兴趣的:(java)