Docx4j 加载 docx 时出现 NullPointerException
Docx4j loading docx is giving NullPointerException
我想将 docx 转换为 html。我开始编写与 github 中给出的示例相同的代码。这只是加载部分。这本身就是我遇到的问题。
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));
}
}
我收到 NullPointerException。在 github 中查看异常跟踪和源代码导航,我怀疑它与此 class https://github.com/plutext/docx4j/blob/master/src/main/java/org/docx4j/jaxb/Context.java
中的 JAXB 相关的东西有关
Docx4j 源代码位于 https://github.com/plutext/docx4j。
异常跟踪:
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
docx文档不错(Word 2010创建)。我什至解压缩了它以查看 Content_Types.xml 是否存在。就在那里。
我正在使用 Eclipse 和 Java SE 7。我已将所有必需的 jar 文件添加到项目属性中的 Java 构建路径。
请帮帮我。
更新:
实际上,当我将 Context.java 中的这一行添加到我的 class 中以查看是否是问题所在时。
JAXBContext.newInstance("org.docx4j.openpackaging.contenttype");
我可以在我的控制台中看到以下异常:
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.run(Unknown Source)
at java.net.URLClassLoader.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
这对我有用,试试这个
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.docx4j.Docx4J;
import org.docx4j.Docx4jProperties;
import org.docx4j.convert.out.HTMLSettings;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
public class Test {
public static void main(String[] args) throws Docx4JException,
FileNotFoundException {
String inputfilepath = "c:/file.docx";
WordprocessingMLPackage wordMLPackage = Docx4J
.load(new FileInputStream(inputfilepath));
// HTML exporter setup (required)
//.. the HTMLSettings object
HTMLSettings htmlSettings = Docx4J.createHTMLSettings();
htmlSettings.setImageDirPath(inputfilepath + "_files");
htmlSettings.setImageTargetUri(inputfilepath.substring(inputfilepath
.lastIndexOf("/") + 1) + "_files");
htmlSettings.setWmlPackage(wordMLPackage);
OutputStream os = new FileOutputStream(inputfilepath + ".html");
// If you want XHTML output
Docx4jProperties.setProperty("docx4j.Convert.Out.HTML.OutputMethodXML", true);
//Prefer the exporter, that uses a xsl transformation
Docx4J.toHTML(htmlSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
}
}
docx4j 支持几种不同的 JAXB 实现:
- 参考实现
- Sun/Oracle 包含在 Java 6/7/8
- EclipseLink MOXy
如果您想使用 MOXy,您需要:
- 相关的 EclipseLink jar
- docx4j-MOXy-JAXBContext-3.0.0.jar(仅包含 jaxb.properties 个文件)
jaxb.properties 文件只是说:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
如果您使用的是 maven,则只需添加:
<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>
docx4j-MOXy-JAXBContext jar 是否在您的类路径中?要么删除它,要么添加相关的 EclipseLink jar
我想将 docx 转换为 html。我开始编写与 github 中给出的示例相同的代码。这只是加载部分。这本身就是我遇到的问题。
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));
}
}
我收到 NullPointerException。在 github 中查看异常跟踪和源代码导航,我怀疑它与此 class https://github.com/plutext/docx4j/blob/master/src/main/java/org/docx4j/jaxb/Context.java
中的 JAXB 相关的东西有关Docx4j 源代码位于 https://github.com/plutext/docx4j。
异常跟踪:
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
docx文档不错(Word 2010创建)。我什至解压缩了它以查看 Content_Types.xml 是否存在。就在那里。
我正在使用 Eclipse 和 Java SE 7。我已将所有必需的 jar 文件添加到项目属性中的 Java 构建路径。
请帮帮我。
更新:
实际上,当我将 Context.java 中的这一行添加到我的 class 中以查看是否是问题所在时。
JAXBContext.newInstance("org.docx4j.openpackaging.contenttype");
我可以在我的控制台中看到以下异常:
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.run(Unknown Source)
at java.net.URLClassLoader.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
这对我有用,试试这个
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.docx4j.Docx4J;
import org.docx4j.Docx4jProperties;
import org.docx4j.convert.out.HTMLSettings;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
public class Test {
public static void main(String[] args) throws Docx4JException,
FileNotFoundException {
String inputfilepath = "c:/file.docx";
WordprocessingMLPackage wordMLPackage = Docx4J
.load(new FileInputStream(inputfilepath));
// HTML exporter setup (required)
//.. the HTMLSettings object
HTMLSettings htmlSettings = Docx4J.createHTMLSettings();
htmlSettings.setImageDirPath(inputfilepath + "_files");
htmlSettings.setImageTargetUri(inputfilepath.substring(inputfilepath
.lastIndexOf("/") + 1) + "_files");
htmlSettings.setWmlPackage(wordMLPackage);
OutputStream os = new FileOutputStream(inputfilepath + ".html");
// If you want XHTML output
Docx4jProperties.setProperty("docx4j.Convert.Out.HTML.OutputMethodXML", true);
//Prefer the exporter, that uses a xsl transformation
Docx4J.toHTML(htmlSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
}
}
docx4j 支持几种不同的 JAXB 实现:
- 参考实现
- Sun/Oracle 包含在 Java 6/7/8
- EclipseLink MOXy
如果您想使用 MOXy,您需要:
- 相关的 EclipseLink jar
- docx4j-MOXy-JAXBContext-3.0.0.jar(仅包含 jaxb.properties 个文件)
jaxb.properties 文件只是说:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
如果您使用的是 maven,则只需添加:
<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>
docx4j-MOXy-JAXBContext jar 是否在您的类路径中?要么删除它,要么添加相关的 EclipseLink jar