apache POI java.lang.NoClassDefFoundError:org / apache / commons / compress / archivers / zip / ZipFile

时间:2018-09-18 07:24:53

标签: java apache apache-poi

我尝试使用Apache POI 4.0.0制作Word文档,但出现错误

  
    

线程“ AWT-EventQueue-0”中的异常java.lang.NoClassDefFoundError:     org / apache / commons / compress / archivers / zip / ZipFile位于     org.apache.poi.openxml4j.opc.OPCPackage.create(OPCPackage.java:365)       在     org.apache.poi.xwpf.usermodel.XWPFDocument.newPackage(XWPFDocument.java:158)       在     org.apache.poi.xwpf.usermodel.XWPFDocument。(XWPFDocument.java:149)       在model.Model_dokumen.Cetak(Model_dokumen.java:32)在     view.Frame_surat.cetak_dan_simpanActionPerformed(Frame_surat.java:414)       在view.Frame_surat.access $ 400(Frame_surat.java:23)处     view.Frame_surat $ 5.actionPerformed(Frame_surat.java:211)在     javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)       在     javax.swing.AbstractButton $ Handler.actionPerformed(AbstractButton.java:2348)       在     javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)       在     javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)       在     javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)       在java.awt.Component.processMouseEvent(Component.java:6533)在     javax.swing.JComponent.processMouseEvent(JComponent.java:3324)在     java.awt.Component.processEvent(Component.java:6298)在     java.awt.Container.processEvent(Container.java:2238)在     java.awt.Component.dispatchEventImpl(Component.java:4889)在     java.awt.Container.dispatchEventImpl(Container.java:2296)在     java.awt.Component.dispatchEvent(Component.java:4711)在     java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4897)       在     java.awt.LightweightDispatcher.processMouseEvent(Container.java:4534)       在java.awt.LightweightDispatcher.dispatchEvent(Container.java:4475)       在java.awt.Container.dispatchEventImpl(Container.java:2282)在     java.awt.Window.dispatchEventImpl(Window.java:2746)在     java.awt.Component.dispatchEvent(Component.java:4711)在     java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)在     java.awt.EventQueue.access $ 500(EventQueue.java:97)在     java.awt.EventQueue $ 3.run(EventQueue.java:709)在     java.awt.EventQueue $ 3.run(EventQueue.java:703)在     java.security.AccessController.doPrivileged(本机方法),位于     java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)       在     java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)       在java.awt.EventQueue $ 4.run(EventQueue.java:733)在     java.awt.EventQueue $ 4.run(EventQueue.java:731)在     java.security.AccessController.doPrivileged(本机方法),位于     java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)       在java.awt.EventQueue.dispatchEvent(EventQueue.java:730)在     java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)       在     java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)       在     java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)       在     java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)       在     java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)       在java.awt.EventDispatchThread.run(EventDispatchThread.java:82)     造成原因:java.lang.ClassNotFoundException:     org.apache.commons.compress.archivers.zip.ZipFile位于     java.net.URLClassLoader.findClass(URLClassLoader.java:381)在     java.lang.ClassLoader.loadClass(ClassLoader.java:424)在     sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:349)在     java.lang.ClassLoader.loadClass(ClassLoader.java:357)...还有43个     引用

  

我正在按照本站点的教程进行操作:tutorials point,并且我尝试了博客,论坛等提供的任何解决方案,但对我没有任何帮助。 这是我的代码:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import view.Frame_surat;

/**
 *
 * @author nasution
 */
public class Model_dokumen {

    public void Cetak(Frame_surat fs) throws Exception {
        try {
            XWPFDocument document = new XWPFDocument();
//            XWPFDocument document = new XWPFDocument();//blank document
            //membuat paragraf

             //membuat dokumen surat keluar
            try (FileOutputStream outputStream = new FileOutputStream(new File("Surat_keluar.docx"))) {
                //membuat paragraf
                XWPFParagraph paragraf = document.createParagraph();
                XWPFRun run = paragraf.createRun();
                run.setText("ini adalah halaman dokumen yg baru saja dibuat menggunakan POI");
                document.write(outputStream);
            }
            JOptionPane.showMessageDialog(null, "Surat keluar berhasil dibuat");
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Model_dokumen.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Model_dokumen.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

enter image description here

enter image description here

enter image description here

4 个答案:

答案 0 :(得分:3)

对于 maven 用户,你可以很容易地从包名中推断出它缺少一个 apache commons-compress lib

https://mvnrepository.com/artifact/org.apache.commons/commons-compress

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-compress</artifactId>
    <version>1.20</version>
</dependency>

答案 1 :(得分:2)

尝试搜索类名称,然后下载jar,您可能会找到指定的jar文件。 “例如:XWPF jar文件下载。”并将XWPF的类下载为jar文件,我之前曾遇到过此问题,并且我这样解决了它,但对我来说是pdfBox。 PS:尝试在jar文件中搜索“ org.apache.poi.xwpf”,您将获得更多结果。

答案 2 :(得分:1)

在带有 AGP 7.0 的 Android 上得到这个,但出现以下错误:

<块引用>
  • 出了什么问题: 无法确定任务“:app:mergeDebugAssets”的依赖关系。 > 无法解析配置“:app:debugRuntimeClasspath”的所有依赖项。
    > 无法创建任务“:app:minifyReleaseWithR8”。
    > 'void org.apache.commons.compress.archivers.zip.ZipFile.(java.nio.channels.SeekableByteChannel)'

根本原因是我没有安装正确的构建工具版本。

所以 sdkmanager 'build-tools;<buildToolsVersion>' 为我解决了这个问题(其中 buildToolsVersion 是您在 build.gradle(s) 中使用的版本,在我的情况下为 30.0.3)

答案 3 :(得分:0)

对于poi-4.1.0,我必须将它们添加到类路径中:     dom4j-1.6.jar     xmlbeans-3.1.0.jar     commons-collections4-4.3.jar     commons-compress-1.18.jar 只有这样,此功能才能用于Excel.xlsx文件:

ExcelWBook = new XSSFWorkbook(ExcelFile);