【发布时间】:2021-09-23 03:23:32
【问题描述】:
我有一个小项目,当我让它运行时确实会引发异常。
问题出在这里(TestObject.java):
final JAXBContext jaxbContext = JAXBContext.newInstance(...);
我真的不明白为什么这会引发异常。我还创建了一个像魅力一样工作的测试(TestTest.java)。请原谅我的名字,但这只是一个小型测试应用程序,可以让它与 java 11 一起使用。
我关注了这个:javax.xml.bind.JAXBException Implementation of JAXB-API has not been found on module path or classpath 并添加了
compile('javax.xml.bind:jaxb-api:2.3.0')
compile('javax.activation:activation:1.1')
compile('org.glassfish.jaxb:jaxb-runtime:2.3.0')
到我的 build.gradle 但它抱怨说
Error:java: module not found: java.activation
不见了。由于这也已从 Java 11 中删除,因此我添加了:
compile 'com.sun.activation:javax.activation:1.2.0'
但它会引发很多错误:
Error:java: the unnamed module reads package com.sun.xml.bind from both jaxb.runtime and jaxb.core
Error:java: the unnamed module reads package com.sun.xml.bind.util from both jaxb.runtime and jaxb.core
Error:java: the unnamed module reads package com.sun.xml.bind.marshaller from both jaxb.runtime and jaxb.core
Error:java: the unnamed module reads package com.sun.xml.bind.api from both jaxb.runtime and jaxb.core
Error:java: the unnamed module reads package com.sun.xml.bind.v2 from both jaxb.runtime and jaxb.core
Error:java: the unnamed module reads package com.sun.xml.bind.v2.util from both jaxb.runtime and jaxb.core
Error:java: the unnamed module reads package com.sun.xml.bind.v2.model.impl from both jaxb.runtime and jaxb.core
Error:java: the unnamed module reads package com.sun.xml.bind.v2.model.annotation from both jaxb.runtime and jaxb.core
Error:java: the unnamed module reads package com.sun.xml.bind.v2.runtime from both jaxb.runtime and jaxb.core
Error:java: the unnamed module reads package com.sun.xml.bind.v2.runtime.unmarshaller from both jaxb.runtime and jaxb.core
Error:java: the unnamed module reads package com.sun.xml.bind.unmarshaller from both jaxb.runtime and jaxb.core
Error:java: module jaxb.runtime reads package com.sun.xml.bind from both jaxb.core and jaxb.runtime
Error:java: module jaxb.runtime reads package com.sun.xml.bind.util from both jaxb.core and jaxb.runtime
Error:java: module jaxb.runtime reads package com.sun.xml.bind.marshaller from both jaxb.core and jaxb.runtime
Error:java: module jaxb.runtime reads package com.sun.xml.bind.api from both jaxb.core and jaxb.runtime
Error:java: module jaxb.runtime reads package com.sun.xml.bind.v2 from both jaxb.core and jaxb.runtime
Error:java: module jaxb.runtime reads package com.sun.xml.bind.v2.util from both jaxb.core and jaxb.runtime
Error:java: module jaxb.runtime reads package com.sun.xml.bind.v2.model.impl from both jaxb.core and jaxb.runtime
Error:java: module jaxb.runtime reads package com.sun.xml.bind.v2.model.annotation from both jaxb.core and jaxb.runtime
Error:java: module jaxb.runtime reads package com.sun.xml.bind.v2.runtime from both jaxb.core and jaxb.runtime
Error:java: module jaxb.runtime reads package com.sun.xml.bind.v2.runtime.unmarshaller from both jaxb.core and jaxb.runtime
Error:java: module jaxb.runtime reads package com.sun.xml.bind.unmarshaller from both jaxb.core and jaxb.runtime
Error:java: module jaxb.core reads package com.sun.xml.bind from both jaxb.core and jaxb.runtime
让我感到困惑的是:测试有效。所以似乎在 junit5 环境中有某种 jaxb 实现。 我还发现了有关如何将 java 8 应用程序迁移到 java 9 模块的一些其他信息,我在其中找到了这个:
compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.4.0-b180830.0359'
compile group: 'org.glassfish.jaxb', name: 'jaxb-runtime', version: '2.4.0-b180830.0438'
compile group: 'org.glassfish.jaxb', name: 'jaxb-xjc', version: '2.4.0-b180830.0438'
但这也行不通。如果有人可以帮助我运行它,我会非常高兴。 我在 Fedora 29 64 位上使用 java-openjdk-11.0.1.13-11.rolling.fc29.x86_64。
ps.:我想将所有这些都写到主题为“javax.xml.bind.JAXBException...”的帖子的评论中,但我不被允许这样做。所以我创建了这个新帖子。
编辑:该示例应用程序中引发的异常是:
Exception in thread "main" java.lang.RuntimeException: javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
- with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory]
at test/com.example.TestObject.doSomething(TestObject.java:21)
at test/com.example.TestObject.main(TestObject.java:12)
Caused by: javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
- with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory]
at java.xml.bind/javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:269)
at java.xml.bind/javax.xml.bind.ContextFinder.find(ContextFinder.java:412)
at java.xml.bind/javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:721)
at java.xml.bind/javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:662)
at test/com.example.TestObject.doSomething(TestObject.java:17)
... 1 more
Caused by: java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at java.xml.bind/javax.xml.bind.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:122)
at java.xml.bind/javax.xml.bind.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:155)
at java.xml.bind/javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:267)
... 5 more
【问题讨论】:
-
在 java 11 中,这些库已从 JDK 中删除。您需要将库添加为依赖项。检查这个:blog.codefx.org/java/java-11-migration-guide/…
-
@NeplatnyUdaj 我已经添加了这些依赖项。否则,应用程序将无法编译。出于这个原因,我添加了 org.glassfish.jaxb 人工制品。如果这些是错误的,请告诉我使用哪一个。
-
我按照上面第一条评论中的说明进行操作。将 jaxb-core 和 jaxb-impl 添加到我的 lib 目录解决了我的问题。
标签: java java-11 java-platform-module-system