Results 1 to 1 of 1
  1. #1
    wildheart25c is offline Member
    Join Date
    Sep 2009
    Posts
    13
    Rep Power
    0

    Default APACHE POI 2007 word documents

    As fas as I know, the following is all you need to print on a screen the contents of a 2007 ms word document.

    Java Code:
    package javaapplication9;
    
    import java.io.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import org.apache.poi.xwpf.usermodel.XWPFDocument;
    import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
    
    public class Main
     {
        public static void main(String[] args)
         {
            XWPFDocument doc;
            try
             {
                doc = new XWPFDocument(new FileInputStream("C:\\documents and settings\\x\\desktop\\1.docx"));
                XWPFWordExtractor wordxExtractor = new XWPFWordExtractor(doc);
                System.out.println(wordxExtractor.getText());
             }
    
            catch (IOException ex)
             {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
             }
         }
    }
    i added the following jar files to the library (setting classpath) which can be reached by right clicking on the project, selecting properties, then libraries, then add jar/folder:
    dom4j-1.6.1.jar
    xmlbeans-2.3.0.jar
    poi-3.8-beta3-20110606.jar
    poi-ooxml-3.83-20110606.jar

    i got these jar files from http://www.apache.org/dyn/closer.cgi...3-20110606.zip just extract the contents to a folder. thts it. Easy.

    So why won't it work????? :(
    This is the exception I'm getting:
    Exception in thread "main" org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
    at org.apache.poi.xwpf.usermodel.XWPFFactory.createDo cumentPart(XWPFFactory.java:66)
    at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocum entPart.java:293)
    at org.apache.poi.POIXMLDocument.load(POIXMLDocument. java:186)
    at org.apache.poi.xwpf.usermodel.XWPFDocument.<init>( XWPFDocument.java:115)
    at javaapplication9.Main.main(Main.java:17)
    Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Construc tor.java:513)
    at org.apache.poi.xwpf.usermodel.XWPFFactory.createDo cumentPart(XWPFFactory.java:63)
    ... 4 more
    Caused by: java.lang.NoClassDefFoundError: org/openxmlformats/schemas/wordprocessingml/x2006/main/StylesDocument$Factory
    at org.apache.poi.xwpf.usermodel.XWPFStyles.onDocumen tRead(XWPFStyles.java:84)
    at org.apache.poi.xwpf.usermodel.XWPFStyles.<init>(XW PFStyles.java:65)
    ... 9 more
    Caused by: java.lang.ClassNotFoundException: org.openxmlformats.schemas.wordprocessingml.x2006. main.StylesDocument$Factory
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 48)
    ... 11 more

    EDIT: Fixed it. I rarely look at the CAUSED BY clause because I always get my information from that first paragraph when it points to the line number in my code. I noticed CLASSDEFNOTFOUND. I've faced an error like this when I started this project. So I looked for org/openxmlformats/schemas/wordprocessingml/x2006/main/StylesDocument$Factory and learned that it was in the ool schemas folder. So now we have one more jar file to include on top of the list ia lready printed. This file is also find inside the zip you downloaded:
    poi-ooxml-schemas-3.8-beta3-20110606.jar


    Well hopefully when I edit my original project and add this, I won't have to bother you again. Maybe someone will learn from this. Good luck and thank you.
    Last edited by wildheart25c; 06-20-2011 at 06:03 AM.

Similar Threads

  1. Read Bookmarks from a word document using apache POI
    By anuskesh in forum Advanced Java
    Replies: 0
    Last Post: 07-28-2010, 04:36 AM
  2. Replies: 0
    Last Post: 11-15-2008, 07:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •