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

9

46

I am getting the error below while running my eclipse plugin. Eclipse used 4.12 with java11. It used to work fine in 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)
Purulent answered 19/9, 2019 at 10:31 Comment(4)
A plug-in that you have installed into Eclipse (com.my.cep.studio?) makes use of JAXB, which is contained in Java 8, but not in Java 11. Running Eclipse with Java 8 should fix your issue (you can still use Java 11 for your projects in Eclipse).Dillie
@howler com.my.cep.studio is my custom writen plugin. The exception occurs from eclipse internal plugin "javax.xml.bind_2.2.0.v201105210648.jar". This is same as jaxb jar and same code inside. The exception occurs from class ContextFinder find method at line return newInstance(contextPath, contextPathClasses, PLATFORM_DEFAULT_FACTORY_CLASS, classLoader, properties). Here PLATFORM_DEFAULT_FACTORY_CLASS points to com.sun.xml.internal.bind.v2.ContextFactoryPurulent
The Eclipse platform does not require or contain the JAXB plug-in (only some Eclipse IDE packages contain the javax.xml.bind plug-in). Probably a plug-in/bundle is missing in your dependencies. Show how javax.xml.bind.ContextFinder is included, and find out which missing plug-in contains com.sun.xml.internal.bind.v2.ContextFactory.Dillie
All you need to go through this answer #48204641Deimos
B
74

Sun people have remove directly access to jaxb package in java 11.
These dependency will work instead of it. Same fix 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>
implementation 'com.sun.xml.bind:jaxb-core:2.3.0.1'
implementation 'javax.xml.bind:jaxb-api:2.3.1'
implementation 'com.sun.xml.bind:jaxb-impl:2.3.1'
implementation 'org.javassist:javassist:3.25.0-GA'
Burwell answered 18/4, 2020 at 1:26 Comment(1)
In case anyone struggles to find out why dependencies themselves did not help - make sure to check the imports. The code I worked on used old javax.xml.bind imports - when changed to jakarta.xml.bind the exception has gone.Kareenkarel
D
14

I've stumbled upon this question while migrating a big ancient Java application to Java 11. I think the following might come useful.

I had everything in place in my Maven POMs, javax bindings, JAXB's implementation, a few tests working fine. Yet, the original application's tests kept inexplicably complaining that com.sun.xml.internal.bind.v2.ContextFactory is not found.

Indeed, that class isn't in any 3rd-party implementations of JAXB (I'm using the Glassfish's one, but there are several others), and one has to replace the default factory that is instantiated inside javax.xml.bind.JAXBContext.newInstance( <classes> ) with some new implementation (com.sun.xml.bind.v2.JAXBContextFactory in my case and, I guess, in other JAXB libs too).

And well, one of the ways to set that is via the jaxb.properties file, which must be visible to the classpath (so, in Maven, it's usually somewhere like src/main/resources/*). As it is explained here, this file has to contain:

javax.xml.bind.context.factory=com.sun.xml.bind.v2.JAXBContextFactory

In my case, it was containing the old now-disappeared factory. I guess, current JAXB libraries use a correct default factory if they don't meet that file, however it might be already present in your codebase and you might need a while before realising it. I hope that this answer will save you the time I wasted.

Dentition answered 18/2, 2020 at 17:45 Comment(0)
G
11

Yes, you are using features in javax that existed in java 8 but are removed in Java 11

see this answer How to resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException in Java 9

Glassco answered 19/9, 2019 at 10:53 Comment(0)
V
5

These libs worked for me (inspired by Vivek's response above). When I tried using JAXB sample from Vogella tutorial with 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'
}
Vivid answered 27/10, 2020 at 5:20 Comment(1)
compile 'com.sun.xml.bind:jaxb-core:2.3.0.1' and compile 'com.sun.xml.bind:jaxb-impl:2.3.1' solved the problemCurler
S
5

For me, these 2 lines in the build.gradle worked for me.

compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.1'
compile group: 'org.glassfish.jaxb', name: 'jaxb-runtime', version: '2.3.1'
Spatz answered 23/3, 2021 at 5:49 Comment(0)
I
4

I was strugling with the same problem. The jakarta.xml.bind implementation will want to use the com.sun.xml.bind.v2.ContextFactory instead of com.sun.xml.internal.bind.v2.ContextFactory which is available in the com.sun.xml.bind. So that combo worked for me.

Ivar answered 16/11, 2020 at 17:42 Comment(0)
N
1

Adding these dependancies in my pom solved the 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>
Natheless answered 23/7, 2023 at 5:17 Comment(0)
L
0

I had to create and add the following file in my project
META-INF/services/javax.xml.bind.JAXBContext
with the following content:

com.sun.xml.bind.v2.ContextFactory

enter image description here

This tells the API which implementation to use.

I was using the following runtime implementation:

<!-- https://mvnrepository.com/artifact/org.glassfish.jaxb/jaxb-runtime -->
<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>2.3.1</version>
</dependency>
Labourite answered 20/2, 2023 at 10:45 Comment(0)
G
0

Instead of using javax and sun try using following ie jakarta

    <!-- API, java.xml.bind module -->
    <dependency>
        <groupId>jakarta.xml.bind</groupId>
        <artifactId>jakarta.xml.bind-api</artifactId>
    </dependency>

    <!-- Runtime, com.sun.xml.bind module -->
    <dependency>
        <groupId>org.glassfish.jaxb</groupId>
        <artifactId>jaxb-runtime</artifactId>
    </dependency>

earlier with error had following package

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.1</version> <!-- Replace with the appropriate version -->
</dependency>

<dependency>
 <groupId>javax.xml.bind</groupId>
 <artifactId>jaxb-api</artifactId>
 <version>2.3.1</version> <!-- Replace with the appropriate version -->
</dependency>

and my code for parsing will be importing following which is working

import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Unmarshaller;

and code for parsing as follows :

JAXBContext jaxbContext = JAXBContext.newInstance(Batch.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            Batch batch = (Batch) jaxbUnmarshaller.unmarshal(new StringReader(xmlString));
            logger.info("parseXml was successful");
            return batch;

Where batch is the object to which xml has to be read into/ or mapped into

Grose answered 23/5 at 11:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.