Eclipse 4.12-java.lang.ClassNotFoundException:com.sun.xml.internal.bind.v2.ContextFactory

时间:2019-09-19 10:31:48

标签: eclipse eclipse-plugin eclipse-rcp java-11

运行eclipse插件时出现错误。 Eclipse在Java11中使用4.12。过去在eclipse 4.6.3 + java8中可以正常工作。

javax.xml.bind.JAXBException: Provider com.sun.xml.internal.bind.v2.ContextFactory not found
 - with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory cannot be found by com.my.cep.studio.cluster.topology_0.0.0]

!ENTRY com.my.cep.diagramming 4 0   at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:146)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:289)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:412)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:375)
    at com.my.cep.studio.cluster.topology.editors.ClusterTopologyDiagramManager.parseFile(ClusterTopologyDiagramManager.java:193)
    at com.my.cep.studio.cluster.topology.editors.ClusterTopologyDiagramManager.openModel(ClusterTopologyDiagramManager.java:175)
    at com.my.cep.diagramming.drawing.BaseDiagramManager.createDrawingCanvas(BaseDiagramManager.java:233)
    at com.my.cep.diagramming.drawing.DiagramManager$2.run(DiagramManager.java:259)
    at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Caused by: java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory cannot be found by com.my.cep.studio.cluster.topology_0.0.0
    at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:512)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:423)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:415)
    at org.eclipse.osgi.internal.loader.ModuleClassLoader.loadClass(ModuleClassLoader.java:155)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    at javax.xml.bind.ContextFinder.safeLoadClass(ContextFinder.java:480)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:112)

6 个答案:

答案 0 :(得分:15)

Sun people have remove directly access to jaxb package in java 11.
These dependency will work instead of it.
And same time if if you are facing hibernate "NullPointerException" issue.


<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-core</artifactId>
    <version>2.3.0.1</version>
</dependency>
<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.1</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.3.1</version>
</dependency>
<dependency>
    <groupId>org.javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.25.0-GA</version>
</dependency>

答案 1 :(得分:3)

在将big ancient Java application迁移到Java 11时,我偶然发现了这个问题。我认为以下内容可能会有用。

我在Maven POM,javax绑定,JAXB的实现,一些测试都能正常工作的所有地方都准备就绪。但是,原始应用程序的测试始终莫名其妙地抱怨找不到com.sun.xml.internal.bind.v2.ContextFactory

实际上,该类不在JAXB的任何第三方实现中(我使用的是Glassfish的,但是还有其他几种),并且其中一个必须替换在{{1}中实例化的默认工厂。 }具有一些新的实现(在我的情况下,javax.xml.bind.JAXBContext.newInstance( <classes> ),在其他JAXB库中也是如此)。

好吧,一种设置方法是通过com.sun.xml.bind.v2.JAXBContextFactory文件,该文件必须是类路径可见的(因此,在Maven中,它通常类似于jaxb.properties)。正如here所述,该文件必须包含:

src/main/resources/*

就我而言,它包含了一个现已消失的旧工厂。我猜想,如果当前的JAXB库不符合该文件,则会使用正确的默认工厂,但是该文件可能已经存在于您的代码库中,并且您可能需要一段时间才能实现它。我希望这个答案可以节省您的时间。

答案 2 :(得分:1)

是的,您正在使用Java 8中已存在但在Java 11中已删除的Javax中的功能

查看此答案 How to resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException in Java 9

答案 3 :(得分:0)

这些库为我工作(受上述Vivek的响应启发)。当我尝试将JAXB sample from Vogella tutorial与Intellij结合使用时。

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile group: 'org.eclipse.persistence', name: 'org.eclipse.persistence.moxy',version: '2.7.3'
    compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.1'
    compile 'com.sun.xml.bind:jaxb-core:2.3.0.1'
    compile 'com.sun.xml.bind:jaxb-impl:2.3.1'
    compile 'org.javassist:javassist:3.25.0-GA'
}

答案 4 :(得分:0)

我在同一个问题上挣扎。 jakarta.xml.bind 实现将要使用 com.sun.xml.bind.v2.ContextFactory 而不是 com.sun.xml.internal。 com.sun.xml.bind 中提供的bind.v2.ContextFactory 。这样那个组合对我有用。

答案 5 :(得分:0)

对我来说,build.gradle 中的这两行对我有用。

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