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

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


当前回答

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

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

其他回答

为了解决这个问题,我在我的项目中导入了一些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文件

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

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

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

SBT解决方案

libraryDependencies += "javax.xml.bind" % "jaxb-api" % "2.3.1"

这对我很管用。只添加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>

对我来说,这是一个简单的解决方案(Mac用户)

run in terminal——> alias j8="export JAVA_HOME= ' /usr/libexec/ JAVA_HOME -v 1.8 '; 然后执行——> j8 就是这样!!(现在运行你的MVN命令)

或者你可以在你的.bash_profile中设置