Search code examples
javafilenotfoundexceptiondocx4j

Docx4j gives error when I try to read content of existing docx file


I am trying to read the content of a docx file from my system using Docx4Java. I have searched enough for the answer but unfortunately couldn't find one.

Below is the error I got while I tried to implement my code.

java.io.FileNotFoundException: G:\WorkSpaces\111.docx (The system cannot find the file specified)

PS : There is no mistake in providing file path. No jar file is absent. I have checked everything before asking.

Can someone please tell me where am I going wrong ?

import java.io.*;
import java.util.*;
import org.docx4j.*;

public class doc4jcodegeeks {
   public static void main(String[] args) throws FileNotFoundException {
    try {
        doc4jcodegeeks dcf = new doc4jcodegeeks();
        dcf.getTemplate();
        }
        catch (Exception e) {
        e.printStackTrace();
       }
    }

    private WordprocessingMLPackage getTemplate() throws Docx4JException, FileNotFoundException {
    WordprocessingMLPackage template = WordprocessingMLPackage.load(new FileInputStream(
            new File("G:\\WorkSpaces\\111.docx")));
    return template;
}

Solution

  • Thanks for your answer Ken Bekov. After some time, I figured out the solution and displayed document's contents on output window in the following way :

    private WordprocessingMLPackage getTemplate() throws Docx4JException, FileNotFoundException {
        WordprocessingMLPackage template = WordprocessingMLPackage.load(new java.io.File("G:\\WorkSpaces\\111.docx"));
        MainDocumentPart documentPart = template.getMainDocumentPart();
    
        List<Object> listObj = documentPart.getContent();
    
         String str = listObj.toString();
         System.out.println(str);
    
        return template;
    }