我有一些使用JAXB API类的代码,这些类已作为Java 6/7/8中的JDK的一部分提供。当我用Java 9运行相同的代码时,在运行时得到错误,指示找不到JAXB类。

从Java 6开始,JAXB类就作为JDK的一部分提供了,那么为什么Java 9再也找不到这些类了呢?


当前回答

旧答案“问题通过切换到amazoncorretto解决” 新闻回答:我使用了最新的corretto,但类似jdk 1.8。无论如何,我们需要手动添加依赖项

其他回答

我知道我迟到了,但我的错误最终需要一个不同的解决方案……也非常简单

我最初部署到Tomcat 9,然后意识到我需要7…我忘记将我的类路径映射回build.xml中的7版本

希望这将在将来修复其他人的错误,谁设法忽略这个简单的问题,就像我一样!

这对我很管用。只添加jaxb-api是不够的。

        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>${jaxb-api.version}</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>${jaxb-api.version}</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
            <version>${jaxb-api.version}</version>
        </dependency>

在我的例子中(spring boot fat jar),我只是将以下内容添加到pom.xml。

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.1</version>
</dependency>

添加下面的依赖对我来说是有效的。

        <!-- API, java.xml.bind module -->
    <dependency>
        <groupId>jakarta.xml.bind</groupId>
        <artifactId>jakarta.xml.bind-api</artifactId>
        <version>2.3.2</version>
    </dependency>

    <!-- Runtime, com.sun.xml.bind module -->
    <dependency>
        <groupId>org.glassfish.jaxb</groupId>
        <artifactId>jaxb-runtime</artifactId>
        <version>2.3.2</version>
    </dependency>

对于Java Web Start执行,我们可以像这样使用Andy Guibert的建议:

<j2se version="1.6+" 
      java-vm-args="-XX:+IgnoreUnrecognizedVMOptions --add-modules=java.se.ee"/>

注意——add-modules中额外的“=”。请参阅此OpenJDK票据或Java平台的“理解运行时访问警告”中的最后一个注释,标准版Oracle JDK 9迁移指南。