0

When I try to extract images from an MS Word document using Java, I get the following exception:

"Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/poifs/filesystem/FileMagic"

package UFC;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.apache.poi.xwpf.usermodel.XWPFPictureData;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

public class WordImageExtractor {

    public static void main(String[] args) {

        selectwORD();

    }

    //allow office word file selection for extracting
    public static void selectwORD() {

        JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("DOCX", "docx");
        chooser.setFileFilter(filter);
        chooser.setMultiSelectionEnabled(false);
        int returnVal = chooser.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = chooser.getSelectedFile();
            System.out.println("Please wait...");
            extractImages(file.toString());
            System.out.println("Extraction complete");
        }

    }

    public static void extractImages(String src) {
        try {

            //create file inputstream to read from a binary file
            FileInputStream fs = new FileInputStream(src);
            //create office word 2007+ document object to wrap the word file
            XWPFDocument docx = new XWPFDocument(fs);
            //get all images from the document and store them in the list piclist
            List<XWPFPictureData> piclist = docx.getAllPictures();
            //traverse through the list and write each image to a file
            Iterator<XWPFPictureData> iterator = piclist.iterator();
            int i = 0;
            while (iterator.hasNext()) {
                XWPFPictureData pic = iterator.next();
                byte[] bytepic = pic.getData();
                BufferedImage imag = ImageIO.read(new ByteArrayInputStream(bytepic));
                ImageIO.write(imag, "jpg", new File("D:/imagefromword" + i + ".jpg"));
                i++;
            }

        } catch (Exception e) {
            System.exit(-1);
        }

    }

}

I already added the jar files of apache poi to netbeans 8.2,

How can I fix this problem? Please help me.

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
  • 2
    duplicate of https://stackoverflow.com/questions/17973970/how-to-solve-java-lang-noclassdeffounderror – Scary Wombat Jan 16 '18 at 02:59
  • You have two copies of Apache POI on your classpath. See http://poi.apache.org/faq.html#faq-N10006 for help finding the one you didn't want! – Gagravarr Jan 16 '18 at 06:53

0 Answers0