如何在Java 9中解决java.lang.NoClassDefFoundError:javax / xml / bind / JAXBException [英] How to resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException in Java 9

查看:2181
本文介绍了如何在Java 9中解决java.lang.NoClassDefFoundError:javax / xml / bind / JAXBException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some code that uses JAXB API classes which have been provided as a part of the JDK in Java 6/7/8. When I run the same code with Java 9, at runtime I get errors indicating that JAXB classes can not be found.

JAXB类已作为一部分提供自Java 6以来的JDK,为什么Java 9不再能找到这些类?

The JAXB classes have been provided as a part of the JDK since Java 6, so why can Java 9 no longer find these classes?

推荐答案

JAXB API被认为是Java EE API,因此不再包含在Java SE 9中的默认类路径中。在Java 11中,它们完全从JDK中删除。

The JAXB APIs are considered to be Java EE APIs, and therefore are no longer contained on the default class path in Java SE 9. In Java 11 they are completely removed from the JDK.

Java 9引入了概念模块,默认情况下, java.se 聚合模块在类路径(或更确切地说,模块路径)上可用。顾名思义, java.se 聚合模块包含传统上与Java 6/7/8捆绑在一起的Java EE API 。

Java 9 introduces the concepts of modules, and by default the java.se aggregate module is available on the class path (or rather, module path). As the name implies, the java.se aggregate module does not include the Java EE APIs that have been traditionally bundled with Java 6/7/8.

幸运的是,JDK 6/7/8中提供的这些Java EE API仍然在JDK中,但它们默认不在类路径上。以下模块中提供了额外的Java EE API:

Fortunately, these Java EE APIs that were provided in JDK 6/7/8 are still in the JDK, but they just aren't on the class path by default. The extra Java EE APIs are provided in the following modules:

java.activation
java.corba
java.transaction
java.xml.bind  << This one contains the JAXB APIs
java.xml.ws
java.xml.ws.annotation

快速而肮脏的解决方案:(仅限JDK 9/10)

要在运行时使JAXB API可用,请指定以下命令行选项:

- add-modules java.xml.bind

但我仍然需要这适用于Java 8 !!!

如果您尝试使用较旧的JDK指定 - add-modules ,它将会破坏因为这是一个无法识别的选项。我建议使用以下两个选项之一:

But I still need this to work with Java 8!!!
If you try specifying --add-modules with an older JDK, it will blow up becuase it's an unrecognized option. I suggest one of two options:


  1. 您可以通过检查JDK版本在启动脚本(如果有的话)中有条件地应用该参数通过检查 $ JAVA_HOME / release 获取 JAVA_VERSION 属性。

  2. 你可以添加 -XX:+ IgnoreUnrecognizedVMOptions ,使JVM静默忽略无法识别的选项,而不是炸毁。但要小心!您使用的任何其他命令行参数将不再由JVM验证。此选项适用于Oracle / OpenJDK以及IBM JDK(自JDK 8sr4起)

  1. You can conditionally apply the argument in a launch script (if you have one) by inspecting the JDK version by inspecting $JAVA_HOME/release for the JAVA_VERSION property.
  2. You can add the -XX:+IgnoreUnrecognizedVMOptions to make the JVM silently ignore unrecognized options, instead of blowing up. But beware! Any other command line args you use will no longer be validated for you by the JVM. This option works with Oracle/OpenJDK as well as IBM JDK (as of JDK 8sr4)






备用快速解决方案:(仅限JDK 9/10)

请注意,您可以通过指定<$ c $在运行时使所有上述Java EE模块可用c> - add-modules java.se.ee 选项。 java.se.ee 模块是一个聚合模块,包含 java.se.ee 以及上面的Java EE API模块。


Alternate quick solution: (JDK 9/10 only)
Note that you can make all of the above Java EE modules available at run time by specifying the --add-modules java.se.ee option. The java.se.ee module is an aggregate module that includes java.se.ee as well as the above Java EE API modules.

正确的长期解决方案:(所有JDK版本)

上面列出的Java EE API模块都标记为 @Deprecated(forRemoval = true),因为它们是计划删除 jdk / 11 /rel =noreferrer> Java 11 。因此, - add-module 方法将不再适用于Java 11开箱即用。

The Java EE API modules listed above are all marked @Deprecated(forRemoval=true), because they are scheduled for removal in Java 11. So the --add-module approach will no longer work in Java 11 out of the box.

在Java 11和转发中需要做的是在类路径或模块路径上包含您自己的Java EE API副本。例如,您可以将JAX-B API添加为maven依赖项,如下所示:

What you will need to do in Java 11 and forward is include your own copy of the Java EE APIs on the class path or module path. For example, you can add the JAX-B APIs as a maven dependency like this:

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.2.11</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-core</artifactId>
    <version>2.2.11</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.2.11</version>
</dependency>
<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1.1</version>
</dependency>

有关Java模块化的完整详细信息,请参阅 JEP 261:模块系统

For full details on Java modularity, see JEP 261: Module System

这篇关于如何在Java 9中解决java.lang.NoClassDefFoundError:javax / xml / bind / JAXBException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆