繁体   English   中英

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

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

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

线程“AWT-EventQueue-0”中的异常 java.lang.NoClassDefFoundError: org.apache.poi.openxml4j.opc.OPCPackage.create(OPCPackage.java:365) 处的 org/apache/commons/compress/archivers/zip/ZipFile在 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) at view.Frame_surat.cetak_dan_simpanActionPerformed(Frame_surat.java:414) at view.Frame_surat.access$400(Frame_surat.java:23) at view.Frame_surat$5.actionPerformed(Frame_surat.java:211) at 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.Compo nt.processMouseEvent(Component.java:6533) at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6298) at 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) at java.awt.Window.dispatchEventImpl(Window.java:2746) at java.awt.Component.dispatchEvent(Component.java:4711) at 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.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80) 在 java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90) 在 java.security.AccessController.doPrivileged(Native Method) 在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:80) at java.awt.EventQueue.dispatchEvent(EventQueue.java:730) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205) at java.awt.EventDispatchThread.pumpEventThreadsForFilters .java:116) 在 java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.jav) a: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 个

我正在关注这个网站的教程教程点,我尝试了博客、论坛等的任何解决方案,但对我来说没有任何效果。 这是我的代码:

/*
 * 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);
        }
    }

}

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

对于 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);

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

对于 maven 用户,您可以从包名中轻松推断出缺少 apache commons-compress 库

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>

在 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM