本文翻译自:How to resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException in Java 9

I have some code that uses JAXB API classes which have been provided as a part of the JDK in Java 6/7/8. 我有一些使用JAXB API类的代码,这些类作为Java 6/7/8中JDK的一部分提供。 When I run the same code with Java 9, at runtime I get errors indicating that JAXB classes can not be found. 当我使用Java 9运行相同的代码时,在运行时出现错误,指示找不到JAXB类。

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? 自Java 6以来,JAXB类已经作为JDK的一部分提供了,那么Java 9为什么不能再找到这些类?


#1楼

参考:https://stackoom.com/question/2wphK/如何解决java-lang-NoClassDefFoundError-Java-中的javax-xml-bind-JAXBException


#2楼

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. JAXB API被视为Java EE API,因此不再包含在Java SE 9的默认类路径中。在Java 11中,它们已从JDK中完全删除。

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). Java 9引入了模块的概念,默认情况下, java.se聚合模块在类路径(或更确切地说是模块路径)上可用。 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. 顾名思义,该java.se汇聚模块包括那些与Java 6/7/8传统上捆绑了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. 幸运的是,JDK 6/7/8中提供的这些Java EE API仍在JDK中,但默认情况下它们不在类路径中。 The extra Java EE APIs are provided in the following modules: 以下模块提供了额外的Java EE API:

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

Quick and dirty solution: (JDK 9/10 only) 快速而肮脏的解决方案:(仅JDK 9/10)
To make the JAXB APIs available at runtime, specify the following command-line option: 要使JAXB API在运行时可用,请指定以下命令行选项:
--add-modules java.xml.bind

But I still need this to work with Java 8!!! 但是我仍然需要这个才能与Java 8一起使用!!!
If you try specifying --add-modules with an older JDK, it will blow up becase it's an unrecognized option. 如果您尝试使用较旧的JDK指定--add-modules ,它将被炸掉,因为这是无法识别的选项。 I suggest one of two options: 我建议两种选择之一:

  1. You can set any Java 9+ only options using the JDK_JAVA_OPTIONS environment variable. 您可以使用JDK_JAVA_OPTIONS环境变量设置任何仅Java 9+的选项。 This environment variable is automatically read by the java launcher for Java 9+. Java 9+的java启动器会自动读取此环境变量。
  2. You can add the -XX:+IgnoreUnrecognizedVMOptions to make the JVM silently ignore unrecognized options, instead of blowing up. 您可以添加-XX:+IgnoreUnrecognizedVMOptions以使JVM静默忽略无法识别的选项,而不会-XX:+IgnoreUnrecognizedVMOptions But beware! 但是要当心! Any other command line args you use will no longer be validated for you by the JVM. JVM将不再为您验证您使用的任何其他命令行参数。 This option works with Oracle/OpenJDK as well as IBM JDK (as of JDK 8sr4) 该选项与Oracle / OpenJDK以及IBM JDK(自JDK 8sr4起)一起使用

Alternate quick solution: (JDK 9/10 only) 备用快速解决方案:(仅JDK 9/10)
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. 请注意,通过指定--add-modules java.se.ee选项,可以在运行时使上述所有Java EE模块可用。 The java.se.ee module is an aggregate module that includes java.se.ee as well as the above Java EE API modules. java.se.ee模块是一个聚合模块,其中包括java.se.ee以及上述Java EE API模块。 Note, this doesn't work on Java 11 because java.se.ee was removed in Java 11. 请注意,这在Java 11上不起作用,因为在Java 11中已删除java.se.ee


Proper long-term solution: (JDK 9 and beyond) 正确的长期解决方案:(JDK 9和更高版本)

The Java EE API modules listed above are all marked @Deprecated(forRemoval=true) , because they are scheduled for removal in Java 11 . 上面列出的Java EE API模块都标记为@Deprecated(forRemoval=true) ,因为它们计划在Java 11中 删除 。 So the --add-module approach will no longer work in Java 11 out of the box. 因此,-- --add-module方法将不再在Java 11中立即可用。

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. 在Java 11及更高版本中,您需要做的是在类路径或模块路径上包含您自己的Java EE API副本。 For example, you can add the JAX-B APIs as a maven dependency like this: 例如,您可以像这样将JAX-B API添加为Maven依赖项:

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

See the JAXB Reference Implementation page for more details on JAXB. 有关JAXB的更多详细信息,请参见JAXB参考实现页面 。

For full details on Java modularity, see JEP 261: Module System 有关Java模块化的完整详细信息,请参见JEP 261:模块系统。

For Gradle or Android Studio developer: (JDK 9 and beyond) 对于Gradle或Android Studio开发人员:(JDK 9及更高版本)

Add the following dependencies to your build.gradle file: 将以下依赖项添加到您的build.gradle文件中:

dependencies {// JAX-B dependencies for JDK 9+implementation "jakarta.xml.bind:jakarta.xml.bind-api:2.3.2"implementation "org.glassfish.jaxb:jaxb-runtime:2.3.2"
}

#3楼

At the time of compilation as well as run time, add the switch --add-modules java.xml.bind 在编译以及运行时,添加开关--add-modules java.xml.bind

javac --add-modules java.xml.bind <java file name>java --add-modules java.xml.bind <class file>

A good introduction of the JDK 9 modules can also be found at : https://www.youtube.com/watch?v=KZfbRuvv5qc 您也可以在以下网址找到有关JDK 9模块的详细介绍: https : //www.youtube.com/watch?v=KZfbRuvv5qc


#4楼

This worked for me: 这为我工作:

<dependency><groupId>javax.xml.bind</groupId><artifactId>jaxb-api</artifactId><version>2.3.0</version>
</dependency>
<dependency><groupId>org.eclipse.persistence</groupId><artifactId>eclipselink</artifactId><version>2.7.0</version>
</dependency>

Update 更新资料

As @Jasper suggested, in order to avoid depending on the entire EclipseLink library, you can also just depend on EclipseLink MOXy: 正如@Jasper所建议的那样,为了避免依赖整个EclipseLink库,您还可以仅依赖EclipseLink MOXy:

Maven 马文

<dependency><groupId>org.eclipse.persistence</groupId><artifactId>org.eclipse.persistence.moxy</artifactId><version>2.7.3</version>
</dependency>

Gradle 摇篮

compile group: 'org.eclipse.persistence', name: 'org.eclipse.persistence.moxy', version: '2.7.3'

As dependencies for my Java 8 app, which produces a *.jar which can be run by both JRE 8 or JRE 9 with no additional arguments. 作为我的Java 8应用程序的依赖项,该应用程序生成一个* .jar,它可以由JRE 8或JRE 9运行,而无需其他参数。

In addition, this needs to be executed somewhere before JAXB API will be used: 另外,这需要在使用JAXB API之前的某个位置执行:

System.setProperty("javax.xml.bind.JAXBContextFactory", "org.eclipse.persistence.jaxb.JAXBContextFactory");

Works great so far, as a workaround. 到目前为止,效果很好,是一种解决方法。 Doesn't look like a perfect solution though... 虽然看起来不是一个完美的解决方案...


#5楼

For Java Web Start Execution we can use Andy Guibert's suggestion like this: 对于Java Web Start执行,我们可以使用Andy Guibert的建议,如下所示:

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

Note the extra "=" in the --add-modules. 请注意--add-modules中的额外“ =”。 See this OpenJDK Ticket or the last note in "Understanding Runtime Access Warnings" of the Java Platform, Standard Edition Oracle JDK 9 Migration Guide . 请参阅此OpenJDK票证或Java平台标准版《 Oracle JDK 9迁移指南 》的“了解运行时访问警告”中的最后一个注释。


#6楼

You can use --add-modules=java.xml.bind JVM option to add xml bind module to JVM run-time environment. 您可以使用--add-modules=java.xml.bind JVM选项将xml绑定模块添加到JVM运行时环境。

Eg: java --add-modules=java.xml.bind XmlTestClass 例如: java --add-modules=java.xml.bind XmlTestClass

如何解决java.lang.NoClassDefFoundError:Java 9中的javax / xml / bind / JAXBException相关推荐

  1. 处理报错:java/lang/NoClassDefFoundError: java/lang/Object

    操作系统环境:CentOS6.0  2.6.32-220.el6.x86_64 JDK版本环境:jdk1.5.0_22 操作系统原来安装的是jdk1.6,后来开发人员要求java程序使用jdk1.5版 ...

  2. 安装失败java.lang_linux安装jdk出现java/lang/NoClassDefFoundError: java/lang/Object错误的解决方案...

    近日在redhat上安装jdk出现了一个莫名的错误 Error occurred during initialization of VM java/lang/NoClassDefFoundError: ...

  3. 控制台报错:java.lang.ClassNotFoundException: javax.xml.bind.JAXBException之解决方法

    控制台报错:java.lang.ClassNotFoundException: javax.xml.bind.JAXBException之解决方法 参考文章: (1)控制台报错:java.lang.C ...

  4. Java异常处理: 缺包 ClassNotFound javax/xml/bind/JAXBException

    Java异常处理: 缺包 ClassNotFound javax/xml/bind/JAXBException 故障原因分析 JAXB API是java EE 的API,在最新版的Java中(Java ...

  5. spring boot 2.0 java8 下 foundError: javax/xml/bind/JAXBException 解决方法

    spring boot 2.0 java8 下 foundError: javax/xml/bind/JAXBException 解决方法 参考文章: (1)spring boot 2.0 java8 ...

  6. 【已解决】【一眼就会】Exception in thread “main“ java.lang.NoClassDefFoundError java.lang.ClassNo【jar中没有主清单属性】

    <一眼就会系列>每天抽出一点时间巩固基础!同时学习最新知识(与时俱进)!罗列.简述.概括问题,让读者扫一眼就知道该如何处理. 其实就是配置文件和包的版本问题. 解决方法如下:(请放大并用超 ...

  7. nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException

    相信这个问题很多小伙伴已经遇到了,这是在你的jdk版本由8.0升级到9.0之后发生的事情,实际上jdk的版本并不是类似于9.0就会完全兼容8.0的, 但是比如说8.1是完全兼容8.0的8.2是完全兼容 ...

  8. 在CentOS上,Servlet出现java.lang.NoClassDefFoundError

    (1)先给出解决方法: 在本地Window服务器上发布项目,没有问题.但是,迁移到Linux上就错误了,java.lang.NoClassDefFoundError: Could not initia ...

  9. java.lang.NoClassDefFoundError: org/apache/log4j/Level的解决方案

    报的错误是 Failed to instantiate SLF4J LoggerFactory Reported exception: java.lang.NoClassDefFoundError: ...

最新文章

  1. 大整数乘法(POJ2389)
  2. 数据中心业务价值永续的密码——施耐德电气全生命周期服务
  3. RL之Q Learning:利用强化学习之Q Learning实现走迷宫—训练智能体走到迷宫(复杂迷宫)的宝藏位置
  4. 获取浏览器可视区域、屏幕的宽和高
  5. 领导秘书,一般都是什么样的人?
  6. Android--多线程之Handler
  7. 请问如何获取字符串在数组中的位置
  8. Spark集群资源如何分配
  9. gfdgdfgdfg
  10. 台式机U盘安装Centos 安装界面黑屏并且没有网卡驱动
  11. ThinkPHP6 隐藏身份证中间8位
  12. 进阶的阿牛哥之如何存储每日数据到csv或txt文件(如何实现换行)
  13. 解决Windows 10 家庭中文版没有组策略编辑器的问题
  14. 了解RabbitMQ
  15. Html怎样往div中添加文本,给div中添加文本元素
  16. 苹果发布2019款iPad mini/Air:配A12芯片支持手写笔
  17. linux服务器设置jar包开机自启动
  18. 皮皮_ssl2542_并查集
  19. 选型宝访谈:微软CRM X 全球顶尖AI技术=?
  20. Java机选双色球的实现

热门文章

  1. /D _WINDOWS, /D _CONSOLE
  2. 自己动手实现OpenGL-OpenGL原来如此简单(二)
  3. 【剑指offer-Java版】19二叉树的镜像
  4. Android老項目出现javax/xml/bind/JAXBException异常问题解决
  5. 使用Fresco加载图片
  6. linux各目录的文件大小,Linux下查看文件大小和目录大小以及目录下文件的大小...
  7. string类型加减_测试人员应该知道的Redis知识(四) String
  8. Android之ActivityManagerService详解(APP启动过程)
  9. InputStream OutputStream 傻傻分不清
  10. Swift 单元测试