Java 11:在模块路径或类路径上找不到JAXB-API的实现 [英] Java 11: Implementation of JAXB-API has not been found on module path or classpath

查看:304
本文介绍了Java 11:在模块路径或类路径上找不到JAXB-API的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小项目,当我让它运行时确实会引发异常.

I have a little project which does throw an exception when i let it run.

问题在这里(TestObject.java):

The problem is here (TestObject.java):

final JAXBContext jaxbContext = JAXBContext.newInstance(...);

我真的不明白为什么这会引发异常.我还创建了一个像超级按钮一样工作的测试(TestTest.java).请原谅我的名字,但这只是一个小的测试应用程序,它可以与Java 11一起使用.

I really do not understand why this throws the exception. I also created a test which works like a charm (TestTest.java). Please forgive me the names, but this is just a small test application to get this working with java 11.

我遵循以下步骤:

I followed this: javax.xml.bind.JAXBException Implementation of JAXB-API has not been found on module path or classpath and added

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,但是它抱怨

to my build.gradle but then it complains that

Error:java: module not found: java.activation

丢失.由于这也已从Java 11中删除,因此我添加了:

is missing. As this is also removed from Java 11 i added:

compile 'com.sun.activation:javax.activation:1.2.0'

但是它会引发很多错误:

but then it throws a lot of errors:

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模块的过程中,我还发现了其他一些信息:

What is so confusing to me: the test works. So it seems like there is some kind of jaxb-implementation in the junit5 environment. I also found some other information in how to migrate a java 8 app to java 9 modules, where i found this:

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'

但这也不起作用.如果有人可以帮助我实现目标,我将非常高兴. 我在64位Fedora 29上使用java-openjdk-11.0.1.13-11.rolling.fc29.x86_64.

But this is also not working. I would be very happy if someone can help me to get that running. I use java-openjdk-11.0.1.13-11.rolling.fc29.x86_64 on Fedora 29, 64 bit.

ps .:我想将所有内容写到主题为"javax.xml.bind.JAXBException ..."的帖子的注释中,但我不允许这样做.所以我创建了这个新帖子.

ps.: i wanted to write all that into the comment of the post with topic "javax.xml.bind.JAXBException..." but i was not allowed to do so. So i created this new post.

edit:在该示例应用程序中引发的异常是:

edit: the exception which is thrown in that sample application is this:

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

推荐答案

该解决方案非常简单.首先,使用jaxb 2.3.1. Beta材料仅用于测试. 真正的问题是module-info.java. jaxb必须具有2个条目:

The solution is very easy. First, use jaxb 2.3.1. The beta-stuff is for testing only. The real problem was the module-info.java. It is required to have 2 entries for jaxb:

requires java.xml.bind;
requires com.sun.xml.bind;

第一个要求定义实现,第二个要求定义API.

The first requires defines the implementation, the second the API.

仅需两个依赖项:

compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.1'
compile group: 'org.glassfish.jaxb', name: 'jaxb-runtime', version: '2.3.1'

这篇关于Java 11:在模块路径或类路径上找不到JAXB-API的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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