Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I try to make word document using Apache POI 4.0.0, but i get error

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/commons/compress/archivers/zip/ZipFile at org.apache.poi.openxml4j.opc.OPCPackage.create(OPCPackage.java:365) at org.apache.poi.xwpf.usermodel.XWPFDocument.newPackage(XWPFDocument.java:158) at org.apache.poi.xwpf.usermodel.XWPFDocument.(XWPFDocument.java:149) at 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) 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: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) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Container.dispatchEventImpl(Container.java:2296) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4897) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4534) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4475) at 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) 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:80) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90) 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:80) 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) ... 43 more Blockquote

i following tutorial from this site : tutorials point , and i had trying any solutions from blog, forum, etc. but nothing works for me. This is my code :

/*
 * 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

like image 531
Juliar Nasution Avatar asked Sep 18 '18 07:09

Juliar Nasution


4 Answers

Got this for Android with AGP 7.0, with the following error:

  • What went wrong: Could not determine the dependencies of task ':app:mergeDebugAssets'. > Could not resolve all dependencies for configuration ':app:debugRuntimeClasspath'.
    > Could not create task ':app:minifyReleaseWithR8'.
    > 'void org.apache.commons.compress.archivers.zip.ZipFile.(java.nio.channels.SeekableByteChannel)'

The root reason was that I hadn't installed the right build-tools' version.

So sdkmanager 'build-tools;<buildToolsVersion>' fixed the problem for me (where buildToolsVersion its the version you use in your build.gradle(s), 30.0.3 in my case)

like image 143
crgarridos Avatar answered Nov 13 '22 15:11

crgarridos


For maven users, you can easily deduct from the package name that this is missing an 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>
like image 41
kommradHomer Avatar answered Nov 13 '22 15:11

kommradHomer


For poi-4.1.0 I had to add these to the classpath: dom4j-1.6.jar xmlbeans-3.1.0.jar commons-collections4-4.3.jar commons-compress-1.18.jar Only then did this work for Excel.xlsx File:

ExcelWBook = new XSSFWorkbook(ExcelFile);
like image 6
Chris Liston Avatar answered Nov 13 '22 16:11

Chris Liston


Try searching for the class name then jar download you might find the specified jar files. "ex: XWPF jar files download." and download the classes of XWPF as jar files i got this problem before and I solved it like this but for me it was pdfBox. PS: Try searching for jar files for "org.apache.poi.xwpf" you will get more results.

like image 4
Sparks Avatar answered Nov 13 '22 17:11

Sparks