我正在尝试从here Office Open XML运行教程。我已经下载了Docx4j库并将其作为库添加到netbeans中。
下面给出了代码段
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
public class OfficeOpen {
public static void main(String[] args) throws Docx4JException {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
wordMLPackage.getMainDocumentPart().addParagraphOfText("Hello Word!");
wordMLPackage.save(new java.io.File("src/main/files/HelloWord1.docx"));
}
}
当我运行上面的例子时,我收到以下错误
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at org.docx4j.openpackaging.Base.<clinit>(Base.java:43)
at officeopen.OfficeOpen.main(OfficeOpen.java:24)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
看起来JVM在运行时找不到类,任何建议。我使用的是Netbeans 8.0
答案 0 :(得分:4)
您缺少docx4j所依赖的一些库。请参阅 docx4j依赖关系中的http://htmlpreview.github.io/?https://github.com/plutext/docx4j/blob/master/docs/Docx4j_GettingStarted.html。
您确实缺少docx4j用于记录的 slf4j :http://www.docx4java.org/docx4j/docx4j-3_2_0/dependencies/slf4j-api-1.7.5.jar
可能你需要很多其他人。
我认为这是所有的依赖关系,虽然你可能不需要所有依赖关系:http://www.docx4java.org/docx4j/docx4j-3_2_0/dependencies/