javadoc - java.lang.NoClassDefFoundError:org / docx4j / openpackaging / exceptions / Docx4JException

时间:2018-04-17 14:18:31

标签: java ant apache-poi javadoc docx4j

我试图在源文件上运行(使用ANT)Javadoc以使用docx4j以表格格式生成文档(使用自定义doclet)(也尝试使用Apache POI,同样的错误)。我知道错误的原因(在运行时没有找到类)。运行ANT Build File后,我收到如下控制台错误。我不确定为什么会这样。

控制台错误

javadoc:
  [javadoc] Generating Javadoc
  [javadoc] Javadoc execution
  [javadoc] 1 error
  [javadoc] java.lang.NoClassDefFoundError: org/docx4j/openpackaging/exceptions/Docx4JException
  [javadoc]     at java.lang.Class.getDeclaredMethods0(Native Method)
  [javadoc]     at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
  [javadoc]     at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
  [javadoc]     at java.lang.Class.getMethod0(Class.java:3018)
  [javadoc]     at java.lang.Class.getMethod(Class.java:1784)
  [javadoc]     at com.sun.tools.javadoc.DocletInvoker.invoke(DocletInvoker.java:286)
  [javadoc]     at com.sun.tools.javadoc.DocletInvoker.validOptions(DocletInvoker.java:238)
  [javadoc]     at com.sun.tools.javadoc.Start.parseAndExecute(Start.java:333)
  [javadoc]     at com.sun.tools.javadoc.Start.begin(Start.java:219)
  [javadoc]     at com.sun.tools.javadoc.Start.begin(Start.java:205)
  [javadoc]     at com.sun.tools.javadoc.Main.execute(Main.java:64)
  [javadoc]     at com.sun.tools.javadoc.Main.main(Main.java:54)
  [javadoc] Caused by: java.lang.ClassNotFoundException: org.docx4j.openpackaging.exceptions.Docx4JException
  [javadoc]     at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
  [javadoc]     at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
  [javadoc]     at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
  [javadoc]     ... 12 more
  [javadoc] javadoc: error - fatal error

以下是我正在处理的文件

的build.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="all">

    <target name="all" depends="show, compile, javadoc">
    </target>

    <property environment="env" />
    <property file="build.properties" />

    <path id="master-classpath">
        <fileset dir="lib">
            <include name="*.jar" />
        </fileset>
    </path>

    <target name="compile">
        <mkdir dir="build/classes" />

        <javac srcdir="src" destdir="build/classes">
            <classpath>
                <path refid="master-classpath" />
            </classpath>
        </javac>
    </target>

    <target name="show">
        <echo message="${toString:master-classpath}" />
    </target>

    <target name="javadoc">
        <javadoc access="private" useexternalfile="yes">
            <fileset dir="${cr.srcpath}" />
            <doclet name="com.my.tdd.util.CommentReader" path="build\classes" />
            <classpath>
                <path refid="master-classpath" />
            </classpath>
        </javadoc>
    </target>
</project>

CommentReader.java

package com.my.tdd.util;

import java.io.File;

import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;

import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.MethodDoc;
import com.sun.javadoc.Parameter;
import com.sun.javadoc.RootDoc;

public class CommentReader {
    public static boolean start(RootDoc root) throws Docx4JException {

        // Just trying to generate simple docx file
        WordprocessingMLPackage wordPackage = WordprocessingMLPackage.createPackage();
        MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart();
        mainDocumentPart.addStyledParagraphOfText("Title", "Hello World!");
        mainDocumentPart.addParagraphOfText("Welcome To Baeldung");
        File exportFile = new File("welcome.docx");
        wordPackage.save(exportFile);

        // Table Generation and Other Logic

        return true;
    }
}

/ lib中

enter image description here

的Buildpath

enter image description here

我已经扫描了StackOverflow这个问题,但找不到解决方案。

1 个答案:

答案 0 :(得分:0)

我使用了类似的代码:

public class Doc4jData {

public static void main (String a[]) throws  Exception
{

    WordprocessingMLPackage wordPackage = WordprocessingMLPackage.createPackage();
    MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart();
    mainDocumentPart.addStyledParagraphOfText("Title", "Hello World!");
    mainDocumentPart.addParagraphOfText("Welcome Shashank");
    File exportFile = new File("welcome.docx");
    wordPackage.save(exportFile);
}

我使用了Spring Boot,只需确保使用依赖项即可:

<dependency>
        <groupId>org.docx4j</groupId>
        <artifactId>docx4j</artifactId>
        <version>3.3.5</version>
    </dependency>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.1</version>
    </dependency>

我能够成功生成一个名为“ welcome”的单词文件

enter image description here