0

I'm trying to create docx file in Java using Apache POI. Here is my code..

import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

public class DocGen {



    private static void createDocFile(String fileName) {
        // TODO Auto-generated method stub
        try {
            File file = new File(fileName);
            FileOutputStream fos = new FileOutputStream(file.getAbsolutePath());

            XWPFDocument doc1 = new XWPFDocument();
            XWPFParagraph tempParagraph = doc1.createParagraph();
            XWPFRun tempRun = tempParagraph.createRun();

            tempRun.setText("-----Demo Paragraph-----");
            tempRun.setFontSize(12);
            doc1.write(fos);
            fos.close();
            doc1.close();
            System.out.println("-----------File Created At---------"+file.getAbsolutePath());


        }catch(Exception e) {
            System.out.println("-----Exception------"+e);
        }

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        createDocFile("/home/yashpatel/Documents/Doc1.docx");
        createDocFile("/home/yashpatel/Documents/Doc1.doc");
    }

}

Following are the jar files I have added.. enter image description here

I am getting the following error..

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream 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 DocGen.createDocFile(DocGen.java:17) at DocGen.main(DocGen.java:37) Caused by: java.lang.ClassNotFoundException: org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream 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) ... 5 more

Yash
  • 41
  • 1
  • 6
  • 1
    https://stackoverflow.com/questions/52381075/apache-poi-java-lang-noclassdeffounderror-org-apache-commons-compress-archivers?rq=1 – John Joe Oct 01 '18 at 07:49

0 Answers0