Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docx4j loading docx is giving NullPointerException

I wanted to convert docx to html. I started writing the code same as examples given in github. This is just loading part. There itself I'm getting the problem.

import org.docx4j.Docx4J;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;

public class Main {

    public static void main(String[] args) throws Docx4JException, 
        String inputfilepath = "myfilepathhere";


        OutputStream os = new FileOutputStream(inputfilepath + ".html");

        WordprocessingMLPackage wordMLPackage = Docx4J
                .load(new FileInputStream(inputfilepath));

    }
}

I'm getting NullPointerException. Seeing the exception trace and navigating in source code in github, I suspect it has something to do with JAXB related thing from this class https://github.com/plutext/docx4j/blob/master/src/main/java/org/docx4j/jaxb/Context.java

Docx4j source code is available at https://github.com/plutext/docx4j.

Exception trace:

Exception in thread "main" org.docx4j.openpackaging.exceptions.Docx4JException: Couldn't get [Content_Types].xml from ZipFile
    at org.docx4j.openpackaging.io3.Load3.get(Load3.java:134)
    at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:454)
    at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:371)
    at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:337)
    at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:302)
    at org.docx4j.openpackaging.packages.WordprocessingMLPackage.load(WordprocessingMLPackage.java:170)
    at org.docx4j.Docx4J.load(Docx4J.java:195)
    at Main.main(Main.java:29)
Caused by: org.docx4j.openpackaging.exceptions.InvalidFormatException: Bad [Content_Types].xml
    at org.docx4j.openpackaging.contenttype.ContentTypeManager.parseContentTypesFile(ContentTypeManager.java:713)
    at org.docx4j.openpackaging.io3.Load3.get(Load3.java:132)
    ... 7 more
Caused by: java.lang.NullPointerException
    at org.docx4j.openpackaging.contenttype.ContentTypeManager.parseContentTypesFile(ContentTypeManager.java:679)
    ... 8 more

The docx document is good (created by Word 2010). I've even unzipped it to see if the Content_Types.xml is there. It's there.

I'm using Eclipse and Java SE 7. I've added all the required jar files to Java build path in project properties.

Please help me.

Update:

Actually when I added this line from Context.java into my class to see if that's the problem.

     JAXBContext.newInstance("org.docx4j.openpackaging.contenttype");

I could see the following exception in my console:

    Exception in thread "main" javax.xml.bind.JAXBException: Provider org.eclipse.persistence.jaxb.JAXBContextFactory not found
 - with linked exception:
[java.lang.ClassNotFoundException: org.eclipse.persistence.jaxb.JAXBContextFactory]
    at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
    at javax.xml.bind.ContextFinder.find(Unknown Source)
    at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
    at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
    at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
    at Main.main(Main.java:26)
Caused by: java.lang.ClassNotFoundException: org.eclipse.persistence.jaxb.JAXBContextFactory
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at javax.xml.bind.ContextFinder.safeLoadClass(Unknown Source)
    ... 6 more
like image 599
pinkpanther Avatar asked Dec 07 '25 21:12

pinkpanther


1 Answers

docx4j supports several different JAXB implementations:

  • the reference implementation
  • the one Sun/Oracle include in Java 6/7/8
  • EclipseLink MOXy

If you want to use MOXy, you need:

  1. the relevant EclipseLink jars
  2. docx4j-MOXy-JAXBContext-3.0.0.jar (which just contains the jaxb.properties files)

The jaxb.properties files just say:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

If you are using maven, you'll just need to add:

<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-MOXy-JAXBContext</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.moxy</artifactId>
<version>2.5.1</version>
</dependency>

Is the docx4j-MOXy-JAXBContext jar on your classpath? Either remove it, or add the relevant EclipseLink jars

like image 178
JasonPlutext Avatar answered Dec 09 '25 22:12

JasonPlutext



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!