具有javadoc doclet的自定义XML序列化程序的ClassNotFoundException

tl; dr; XmlOutputFactory使用的类加载器抱怨找不到Woodstox,即使doclet自己的类加载器可以找到它也是如此.

如果在our sample project中运行javadoc目标,我们已将xml工厂设置为使用Woodstox序列化程序,则XmlOutputFactory使用的类加载器会抱怨找不到文件,即使可以在doclet中找到该文件也是如此.

Constructing Javadoc information...
java.net.URLClassLoader@20fa23c1
Check that we can get hold of class: com.ctc.wstx.stax.WstxOutputFactory
Setting javax.xml.stream.XMLOutputFactory to com.ctc.wstx.stax.WstxOutputFactory
Trying to instantiate a new instance from XMLOutputFactory

...
[ERROR] Exit code: 4 - javadoc: error - fatal error encountered: javax.xml.stream.FactoryConfigurationError: Provider com.ctc.wstx.stax.WstxOutputFactory not found
[ERROR] javadoc: error - Please file a bug against the javadoc tool via the Java bug reporting page
[ERROR] (http://bugreport.java.com) after checking the Bug Database (http://bugs.java.com)
[ERROR] for duplicates. Include error messages and the following diagnostic in your report. Thank you.
[ERROR] javax.xml.stream.FactoryConfigurationError: Provider com.ctc.wstx.stax.WstxOutputFactory not found
[ERROR]     at java.xml/javax.xml.stream.FactoryFinder.newInstance(FactoryFinder.java:196)
[ERROR]     at java.xml/javax.xml.stream.FactoryFinder.newInstance(FactoryFinder.java:148)
[ERROR]     at java.xml/javax.xml.stream.FactoryFinder.find(FactoryFinder.java:260)
[ERROR]     at java.xml/javax.xml.stream.FactoryFinder.find(FactoryFinder.java:222)
[ERROR]     at java.xml/javax.xml.stream.XMLOutputFactory.newInstance(XMLOutputFactory.java:138)
[ERROR]     at docs.JacksonWriter.write(JacksonWriter.java:161)
[ERROR]     at docs.TestSheetDoclet.run(TestSheetDoclet.java:98)
[ERROR]     at jdk.javadoc/jdk.javadoc.internal.tool.Start.parseAndExecute(Start.java:588)
[ERROR]     at jdk.javadoc/jdk.javadoc.internal.tool.Start.begin(Start.java:432)
[ERROR]     at jdk.javadoc/jdk.javadoc.internal.tool.Start.begin(Start.java:345)
[ERROR]     at jdk.javadoc/jdk.javadoc.internal.tool.Main.execute(Main.java:63)
[ERROR]     at jdk.javadoc/jdk.javadoc.internal.tool.Main.main(Main.java:52)
[ERROR] Caused by: java.lang.ClassNotFoundException: com/ctc/wstx/stax/WstxOutputFactory

因此,尽管我们的JacksonWriter clearly finds the class(请参见输出),但XmlOutputFactory似乎没有,这表明它具有某种其他类加载器,但看不到提供的类路径.

您可以看到target / site / testapidocs / options中使用的完整类路径:

    -classpath
    '/home/myuser/dev/tmp/server-javadoc-repro/target/classes:/home/myuser/dev/tmp/server-javadoc-repro/target/test-classes:/home/myuser/.m2/repository/org/jetbrains/annotations/17.0.0/annotations-17.0.0.jar:/home/myuser/.m2/repository/com/fasterxml/jackson/module/jackson-module-parameter-names/2.9.7/jackson-module-parameter-names-2.9.7.jar:/home/myuser/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:/home/myuser/.m2/repository/ch/qos/logback/logback-core/1.2.3/logback-core-1.2.3.jar:/home/myuser/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.9.7/jackson-databind-2.9.7.jar:/home/myuser/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.9.0/jackson-annotations-2.9.0.jar:/home/myuser/.m2/repository/org/assertj/assertj-core/3.11.1/assertj-core-3.11.1.jar:/home/myuser/.m2/repository/org/codehaus/woodstox/stax2-api/3.1.4/stax2-api-3.1.4.jar:/home/myuser/.m2/repository/com/fasterxml/jackson/dataformat/jackson-dataformat-xml/2.9.7/jackson-dataformat-xml-2.9.7.jar:/home/myuser/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar:/home/myuser/.m2/repository/org/slf4j/slf4j-api/1.7.25/slf4j-api-1.7.25.jar:/home/myuser/.m2/repository/junit/junit/4.12/junit-4.12.jar:/home/myuser/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.9.7/jackson-core-2.9.7.jar:/home/myuser/.m2/repository/com/fasterxml/jackson/module/jackson-module-jaxb-annotations/2.9.7/jackson-module-jaxb-annotations-2.9.7.jar:/home/myuser/.m2/repository/com/fasterxml/woodstox/woodstox-core/5.0.3/woodstox-core-5.0.3.jar:/home/myuser/.m2/repository/com/fasterxml/jackson/dataformat/jackson-dataformat-xml/2.9.7/jackson-dataformat-xml-2.9.7.jar:/home/myuser/.m2/repository/com/fasterxml/woodstox/woodstox-core/5.0.3/woodstox-core-5.0.3.jar'
    -doclet
    'docs.TestSheetDoclet'
    -docletpath
    '/home/myuser/dev/tmp/server-javadoc-repro/target/acme-server-with-javadoc-issues-0.0.1-SNAPSHOT.jar:/home/myuser/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.9.7/jackson-databind-2.9.7.jar:/home/myuser/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.9.0/jackson-annotations-2.9.0.jar:/home/myuser/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.9.7/jackson-core-2.9.7.jar:/home/myuser/.m2/repository/com/fasterxml/jackson/module/jackson-module-parameter-names/2.9.7/jackson-module-parameter-names-2.9.7.jar:/home/myuser/.m2/repository/com/fasterxml/jackson/dataformat/jackson-dataformat-xml/2.9.7/jackson-dataformat-xml-2.9.7.jar:/home/myuser/.m2/repository/com/fasterxml/jackson/module/jackson-module-jaxb-annotations/2.9.7/jackson-module-jaxb-annotations-2.9.7.jar:/home/myuser/.m2/repository/org/codehaus/woodstox/stax2-api/3.1.4/stax2-api-3.1.4.jar:/home/myuser/.m2/repository/com/fasterxml/woodstox/woodstox-core/5.0.3/woodstox-core-5.0.3.jar:/home/myuser/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar:/home/myuser/.m2/repository/ch/qos/logback/logback-core/1.2.3/logback-core-1.2.3.jar:/home/myuser/.m2/repository/org/slf4j/slf4j-api/1.7.25/slf4j-api-1.7.25.jar:/home/myuser/.m2/repository/org/jetbrains/annotations/17.0.0/annotations-17.0.0.jar:/home/myuser/.m2/repository/com/fasterxml/woodstox/woodstox-core/5.0.3/woodstox-core-5.0.3.jar:/home/myuser/.m2/repository/org/codehaus/woodstox/stax2-api/3.1.4/stax2-api-3.1.4.jar'
    -encoding
    'UTF-8'
    -protected
    -sourcepath
    '/home/myuser/dev/tmp/server-javadoc-repro/src/test/java:/home/myuser/dev/tmp/server-javadoc-repro/target/generated-test-sources/test-annotations'

如何繁殖

git clone https://github.com/fatso83/server-javadoc-repro
cd server-javadoc-repro
mvn install org.apache.maven.plugins:maven-javadoc-plugin:3.0.1:test-javadoc

解决方法:

javax.xml.datatype.FactoryFinder.getProviderClass()的文档(根据OpenJDK 12 33)规定:

    /**
     * Attempt to load a class using the class loader supplied. If that fails
     * and fall back is enabled, the current (i.e. bootstrap) class loader is
     * tried.
     *
     * If the class loader supplied is <code>null</code>, first try using the
     * context class loader followed by the current (i.e. bootstrap) class
     * loader.
     *
     * Use bootstrap classLoader if cl = null and useBSClsLoader is true
     */

因此,如果在调用XMLOutputFactory.newInstance()之前重写线程上下文类加载器,则应该很好:

Thread.currentThread().setContextClassLoader(WstxOutputFactory.class.getClassLoader());
final var xmlOutputFactory = XMLOutputFactory.newInstance();

JAXP有多种不同的方式来配置要在运行时中使用的JAXP实现(或“提供者”),但是最简单的方法是将系统属性javax.xml.stream.XMLOutputFactory设置为要使用的实现类的名称. ,在您的情况下为com.ctc.wstx.stax.WstxOutputFactory:

System.setProperty("javax.xml.stream.XMLOutputFactory", "com.ctc.wstx.stax.WstxOutputFactory");
上一篇:PTA basic 1057 数零壹 (20 分) c++语言实现(g++)


下一篇:凡事预则立