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

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


当前回答

为了解决这个问题,我在我的项目中导入了一些JAR文件:

javax.activation-1.2.0.jar

http://search.maven.org/remotecontent?filepath=com/sun/activation/javax.activation/1.2.0/javax.activation-1.2.0.jar

jaxb-api-2.3.0.jar

http://search.maven.org/remotecontent?filepath=javax/xml/bind/jaxb-api/2.3.0/jaxb-api-2.3.0.jar

jaxb-core-2.3.0.jar

http://search.maven.org/remotecontent?filepath=com/sun/xml/bind/jaxb-core/2.3.0/jaxb-core-2.3.0.jar

jaxb-impl-2.3.0.jar

http://search.maven.org/remotecontent?filepath=com/sun/xml/bind/jaxb-impl/2.3.0/jaxb-impl-2.3.0.jar

下载以上文件并复制到项目的libs文件夹中 在Java Build Path中添加导入的JAR文件

其他回答

为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 -->

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

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

我还碰到了ClassNotFoundException:javax.xml.bind。DatatypeConverter使用Java 11和

<dependency>
    <groupId>io.jsonwebtoken</groupId>
    <artifactId>jjwt</artifactId>
    <version>0.9.1</version>
</dependency>

我尝试了添加javax.xml的所有方法。绑定:jaxb-api或spring boot jakarta.xml。bind-api . .我发现了一个关于jjwt 0.10.0版本修复的提示。但最重要的是,JJWT包现在是分开的!

因此,查看这个参考:https://github.com/jwtk/jjwt/issues/510

很简单,如果你使用

Java11和 jjwt 0.9。x和 你将面临ClassNotFoundException:javax.xml.bind。DatatypeConverter问题,

JJWT 0.11版。X,但是使用分离的包:https://github.com/jwtk/jjwt#install

你的maven找不到更高版本的jjwt依赖,因为他们把包分开了。

欢呼。

我在App中遇到了同样的问题,在模块级别构建。gradle添加了视图绑定和数据绑定。

早些时候

viewBinding {
    enabled = true
}

dataBinding {
    enabled = true
}

解决

dataBinding {
    enabled = true
}

在项目级构建中。Gradle使用如下

classpath 'com.android.tools.build:gradle:3.6.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20"

这解决了我在Java 12上运行Apache Camel 2.24.1的依赖关系的问题:

    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>activation</artifactId>
        <version>1.1.1</version>
    </dependency>

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

    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-core</artifactId>
        <version>2.3.0.1</version>
    </dependency>

    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.3.0.1</version>
    </dependency>