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

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


当前回答

转到您的构建。gradle和添加以下Java 9或Java 10的依赖项。

sourceCompatibility = 10 // You can also decrease your souce compatibility to 1.8 

//java 9+ does not have Jax B Dependents

    compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.0'
    compile group: 'com.sun.xml.bind', name: 'jaxb-core', version: '2.3.0'
    compile group: 'com.sun.xml.bind', name: 'jaxb-impl', version: '2.3.0'
    compile group: 'javax.activation', name: 'activation', version: '1.1.1'

其他回答

我在使用Spring Boot 2.0.5时遇到了同样的问题。在Java 11上发布。

仅仅添加javax.xml.bind:jaxb-api:2.3.0并不能解决这个问题。我还必须将Spring Boot更新到最新的Milestone 2.1.0。M2,所以我认为这将在下一次正式发布中修复。

你只需要一个依赖项:

dependencies {
    implementation ("jakarta.xml.bind:jakarta.xml.bind-api:2.3.2")

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

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

为Java 8目标编译时需要使用的依赖项版本。在Java 8、11和12 JREs中测试的应用程序。

        <!-- replace dependencies that have been removed from JRE's starting with Java v11 -->
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.2.8</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
            <version>2.2.8-b01</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.2.8-b01</version>
        </dependency>
        <!-- end replace dependencies that have been removed from JRE's starting with Java v11 -->

如果你使用的是上面提到的JDK 8,请遵循以下步骤:

添加javax.xml.bind依赖项

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

清洁maven 安装maven