0

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;
}
Vishal A
  • 144
  • 3
  • 17

2 Answers2

0

Seems to be G: is network disk. In windows JVM runs under System user. This user can't see network disks. You can try:

  1. Change user, when you start your program;

  2. Try to specify full network path ( \\share\filename.docx )

  3. At last copy file to local disk;

Ken Bekov
  • 13,696
  • 3
  • 36
  • 44
0

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;
}
Vishal A
  • 144
  • 3
  • 17