0

enter image description hereenter image description hereenter image description here

I am trying to read a docx file using Java, but, for some reason, and after 3 days of error and try, I still can't figure out what is going on with the code. It's a very simple code that reads, as I said, a docx file.

CODE:

 public static void main(String[] args)throws Exception
        {
           XWPFDocument docx = new XWPFDocument(new FileInputStream("C:\\Users\\new_marks.docx"));
           
           //using XWPFWordExtractor Class
           XWPFWordExtractor we = new XWPFWordExtractor(docx);
           System.out.println(we.getText());
        }

Error Msg:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/util/POILogFactory
    at org.apache.poi.ooxml.POIXMLDocumentPart.<clinit>(POIXMLDocumentPart.java:55)
    at meu_docx_reader.Meu_DOCX_reader.main(Meu_DOCX_reader.java:12)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.util.POILogFactory
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    ... 2 more

I have added and removed the jar files over and over and over... And don't know that is going on. I am using eclipse.

Lee Andrew
  • 35
  • 8
  • 2
    Do you have Apache POI in your dependencies? Are you using Maven or Gradle? – Dropout Jul 25 '21 at 20:43
  • Hi... thanks for ur quick reply. I dont know how to use Maven, sorry. :-) And about dependencies, i just added the jar files to my project. – Lee Andrew Jul 25 '21 at 20:45
  • Are you sure they are added correctly? If you use `POILogFactory` somewhere in your code does it want to import the library to the class or does it not know what it is at all? – Dropout Jul 25 '21 at 20:47
  • Thanks so much. I am redoing, for the nth time, my lilipudian project. Lets see if it works now. :-) – Lee Andrew Jul 25 '21 at 20:57

2 Answers2

3

Apache POI library is missing in your project. Try to import the JAR according to this guide: How to import a jar in Eclipse

For the future consider using some build automation to manage your dependencies in a more organized manner - https://stackify.com/gradle-vs-maven/

Dropout
  • 13,653
  • 10
  • 56
  • 109
  • Thanks so much. I am reading it right away. But, how to know, exactly, which dependencies do i need b4hand? – Lee Andrew Jul 25 '21 at 20:56
  • So... Same error... Not sure what is going on... I followed the steps, and same thing...I chose "add external jars" to my build path. – Lee Andrew Jul 25 '21 at 21:00
  • Hard to tell what's going on. When you use `POILogFactory` does it try to create an import in the class you're using it in or does it just mark as something unknown? – Dropout Jul 25 '21 at 21:17
  • Just pasted the error and a list of the jars that i added – Lee Andrew Jul 25 '21 at 21:25
  • Try this JAR: https://repo1.maven.org/maven2/org/apache/poi/poi/5.0.0/poi-5.0.0.jar – Dropout Jul 25 '21 at 21:26
  • What Java version are you using? Looks like there are some restrictions.. Try to find it in Eclipse preferences under Java.. – Dropout Jul 25 '21 at 21:32
  • I am using JRE System Library JAvaSE 14 Win 10 Eclipse Version: 2021-06 (4.20.0) – Lee Andrew Jul 25 '21 at 21:34
  • That's fine.. I'll try to think of something. – Dropout Jul 25 '21 at 21:35
  • 2
    Actually there are a bunch of dependencies that are required for Apache POI 5.0.0 - see https://mvnrepository.com/artifact/org.apache.poi/poi/5.0.0 and scroll down to "compile dependencies".. Try adding them as well to see if it resolves your issue, but frankly I'd strongly advise you to start using either Maven or Gradle to avoid this chaos in the future. It's just a hassle and using build automation makes it 1000x easier since it would automatically download the necessary sub-dependencies. – Dropout Jul 25 '21 at 21:42
  • Again, thanks so much for your help! How would I use Maven in that context? – Lee Andrew Jul 25 '21 at 21:56
  • If you're using Eclipse, i'm pretty sure it supports Maven projects – g00se Jul 25 '21 at 22:08
  • 1
    @LeeAndrew sorry was offline until now, check out this tutorial on how to use maven https://www.baeldung.com/maven – Dropout Jul 26 '21 at 11:21
  • Awesome, Dropout! Thanks a lot! – Lee Andrew Jul 27 '21 at 12:16
0

Soooooo... After 4 days and lots of help... Thank you guys! Dropout and gOOse...

Here is what i did in case someone else needs it too:

1-) I installed Maven in my Eclipse. Eclipse -> Help -> Install New Software. Expand " Collaboration " tag. Select Maven plugin from there. Click on next . Accept the agreement & click finish. (Maven in Eclipse: step by step installation)

2-) I created a brand new project (for the 11th time)... But this time using this tutorial: https://www.youtube.com/watch?v=Rh0vYwJ8RTM

3-) In my pom.xml file I added:

 <dependencies>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>4.1.2</version>
  </dependency>
  </dependencies>

4-) Final step: I solved, by using the import option, all the imports that i needed:

import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
Lee Andrew
  • 35
  • 8
  • 1
    ITS ALIVE!!! Thank you all! Very excited here... Cherio! – Lee Andrew Jul 25 '21 at 22:53
  • 2
    Glad you resolved it. This is why people will so often recommend Maven, Gradle,or a similar _dependency manager_ tool. You have one library (poi-ooxml) in your POM. Maven downloads that for you. And it downloads all the libraries on which that library depends. And it downloads all the libraries on which those libraries depend... and so on, down through the hierarchy of dependencies, until you have every library you need - even if you had no idea you needed them. All from that one entry in your POM. – andrewJames Jul 25 '21 at 23:22
  • Thanks SO MUCH... My life now can be resumed in before and after Maven... ;-) – Lee Andrew Jul 27 '21 at 12:14