Java 11:在模块路径或类路径上找不到 JAXB-API 的实现
我有一个小项目,当我让它运行时,它确实会引发异常。
问题就在这里(TestObject.java):
final JAXBContext jaxbContext = JAXBContext.newInstance(...);
我真的不明白为什么这会引发异常。我还创建了一个像超级按钮一样的测试(TestTest.java)。请原谅我的名字,但这只是一个小型测试应用程序,可以让它与java 11一起使用。
我遵循了这个:javax.xml.bind.JAXBException 在模块路径或类路径上找不到 JAXB-API 的实现并添加
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...”但我不被允许这样做。所以我创建了这个新帖子。
edit:在该示例应用程序中引发的异常是这样的:
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