java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory
Starts a Java project and hits the below "class not found for com.sun.xml.bind.v2.ContextFactory
"?
Terminal
Caused by: java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at jakarta.xml.bind.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:92)
at jakarta.xml.bind.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:125)
at jakarta.xml.bind.ContextFinder.newInstance(ContextFinder.java:230)
... 43 more
Solution
The com.sun.xml.bind.v2.ContextFactory
is under the JAXB APIs; Java 9 deprecated the JAXB, and Java 11 deleted the JAXB completely. To fix it, we have to include the JAXB API and RI dependencies manually.
Note
Since JAXB version 3 and above, the packages also changed from javax.xml.*
to jakarta.xml.*
.
To fix it, we can include the default JAXB RI, Jakarta XML Binding.
pom.xml
<!-- JAXB API only -->
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>3.0.0</version>
</dependency>
<!-- JAXB RI, Jakarta XML Binding -->
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>3.0.0</version>
<scope>runtime</scope>
</dependency>
If we still prefer the old JAXB packages javax.xml.*
, stick with the JAXB version 2.x.
pom.xml
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-ri</artifactId>
<version>2.3.3</version>
</dependency>
Alternatively, we also can include the EclipseLink MOXy, another JAXB RI.
pom.xml
<!-- JAXB API only -->
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>3.0.0</version>
</dependency>
<!-- JAXB RI, EclipseLink MOXy -->
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.moxy</artifactId>
<version>3.0.0</version>
</dependency>
Download Source Code
$ git clone https://github.com/mkyong/core-java
$ cd java-xml
$ cd src/main/java/com/mkyong/xml/jaxb/
Next to StackOverflow, you have provided the most useful information in a brief, clear fashion. Thank you for efforts; they are most appreciated.
Possible this is working but not correct. Because you don’t need to change anything within existing sources. You need to add only proper libraries
I had the same issue on JAVA 11, and solve it by adding this
I tried these along with multiple different options provided here but none worked. It always shows me ContextFactory not found error for JDK 11. Is there any other setting ? in Intellij i do see this ContextFfactory class is available but in runtime, classloader doesn’t find. Any help is appreciated.
thank you, it helped
Excellent article!