JAXBException is the root exception class for all the JAXB exceptions. Generally this exception occurred when the implemented code is not fulfill the pre-requisite for JAXB related dependencies.
Constructors
JAXBException class having following constructors.
- JAXBException(String msg): Construct exception with detail message..
- JAXBException(String msg, String errorCode) : This construct exception with detail message with error code.
- JAXBException(String msg, String errorCode, Throwable exception) : Construct detail message with error code and stacktrace of linked exception stacktrace.
- JAXBException(String msg, Throwable exception) :Construct exception with detail message and stacktrace of linked exception.
- JAXBException(Throwable exception) : Construct exception with stacktrace of linked exception.
Problem
This JAXBException occurred in your code because on runtime JAXB is not able to find JAXB-API related dependency.
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.internal.bind.v2.ContextFactory]
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:278)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:421)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:721)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:662)
at com.facingIssuesOnIT.domain.EmployeeJAXBTest.marshal(EmployeeJAXBTest.java:23)
at com.facingIssuesOnIT.domain.EmployeeJAXBTest.main(EmployeeJAXBTest.java:15)
Caused by: java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at javax.xml.bind.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:122)
at javax.xml.bind.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:155)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:276)
... 5 more
Solutions
To implement JAXB marshalling and unmarshalling in your application, you need to add following dependencies in you code. As above exception message explained “JAXB-API has not found in module or class path” this exception occurred when you forget to add JAXB-API dependency.
<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>
Conclusion
In this post , You learn about the JAXBException cases to occurred, different constructors and example to solve JAXB-API has not found in module or class path” JAXBException.
Let me know your thought to about this post.
Happy Learning !!!