Main Tutorials

JAXBException: Implementation of JAXB-API has not been found on module path or classpath

This article shows how to solve the popular JAXB exception Implementation of JAXB-API has not been found on module path or classpath.

Table of contents

1. JAXBException: Implementation of JAXB-API has not been found

Migrate a project to Java 11 and add a Maven JAXB dependency.

pom.xml

  <dependency>
      <groupId>javax.xml.bind</groupId>
      <artifactId>jaxb-api</artifactId>
      <version>2.3.1</version>
  </dependency>

And hits the below JAXBException?

Terminal

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.mkyong.xml.jaxb.JaxbExample.main(JaxbExample.java:20)
Caused by: java.lang.ClassNotFoundException: com.sun.xml.internal.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 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)
  ... 4 more

2. We need a JAXB Implementation

The JAXB packages javax.xml.* are not visible in Java 9 and Java 10 and removed in Java 11. And the jaxb-api contains only the APIs, and we need the JAXB Implementation.

pom.xml

  <dependency>
      <groupId>javax.xml.bind</groupId>
      <artifactId>jaxb-api</artifactId>
      <version>2.3.1</version>
  </dependency>

3. JAXB Implementation – Jakarta XML Binding

We can add the Jakarta XML Binding as the JAXB Implementation.

pom.xml

  <!-- JAXB API only -->
  <dependency>
      <groupId>jakarta.xml.bind</groupId>
      <artifactId>jakarta.xml.bind-api</artifactId>
      <version>3.0.0</version>
  </dependency>

  <!-- JAXB Implementation -->
  <dependency>
      <groupId>com.sun.xml.bind</groupId>
      <artifactId>jaxb-impl</artifactId>
      <version>3.0.0</version>
      <scope>runtime</scope>
  </dependency>

We also can add EclipseLink MOXy as the JAXB Implementation.

pom.xml

  <!-- JAXB API only -->
  <dependency>
      <groupId>jakarta.xml.bind</groupId>
      <artifactId>jakarta.xml.bind-api</artifactId>
      <version>3.0.0</version>
  </dependency>

  <dependency>
      <groupId>org.eclipse.persistence</groupId>
      <artifactId>org.eclipse.persistence.moxy</artifactId>
      <version>3.0.0</version>
  </dependency>

4.1 Implementation of JAXB-API has not been found?

After added the EclipseLink MOXy JAXB Implementation, we will still hit the same error message.

Terminal

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]

4.2 Need jaxb.properties

For EclipseLink MOXy, we need to add jaxb.properties to one of the packages of the domain class; The jaxb.properties defined JAXB to use the EclipseLink MOXy’s JAXBContextFactory, not the default com.sun.xml.internal.bind.v2.ContextFactory.

jaxb.properties

# For old APIs  2.3.*
# javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

# @Since 3.0, javax.xml rebrand to jakarta.xml
jakarta.xml.bind.JAXBContextFactory=org.eclipse.persistence.jaxb.JAXBContextFactory

4.3 Uses JAXBContextFactory explicitly.

If we don’t want to create a single purpose jaxb.properties file; alternatively, we can explicitly use the EclipseLink MOXy’s JAXBContextFactory to create the JAXBContext.


  // Normal JAXB Implementation
  // JAXBContext jaxbContext = JAXBContext.newInstance(Company.class);

  // EclipseLink MOXy's JAXBContextFactory
  JAXBContext jaxbContext = org.eclipse.persistence.jaxb.JAXBContextFactory
                    .createContext(new Class[] {Company.class}, null);

  Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

5. JAXB Implementation – GlassFish

There is a GlassFish 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>

  <dependency>
      <groupId>org.glassfish.jaxb</groupId>
      <artifactId>jaxb-runtime</artifactId>
      <version>3.0.0</version>
      <scope>runtime</scope>
  </dependency>

6. javax.xml.bind or jakarta.xml.bind?

The Java EE javax.* was rebranded to Jakarta EE jakarta.*, read the history of the Jakarta EE.

6.1 Java EE and javax.xml.*

The jaxb-api contains the APIs in the old Java EE packages javax.*. The JAXB RI also needs to switch to the 2.3.* version.

pom.xml

  <!-- JAXB API -->
  <dependency>
      <groupId>javax.xml.bind</groupId>
      <artifactId>jaxb-api</artifactId>
      <version>2.3.1</version>
  </dependency>

  <!-- JAXB RI -->
  <dependency>
      <groupId>com.sun.xml.bind</groupId>
      <artifactId>jaxb-impl</artifactId>
      <version>2.3.4</version>
  </dependency>

Some APIs in the javax.xml.* package.


// Java EE packages
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

6.2 Jakarta EE and jakarta.xml.*

The jakarta.xml.bind-api* contains the APIs in the new packages jakarta.*. The JAXB RI needs to switch to the 3.* version.

pom.xml

  <!-- JAXB API only -->
  <dependency>
      <groupId>jakarta.xml.bind</groupId>
      <artifactId>jakarta.xml.bind-api</artifactId>
      <version>3.0.0</version>
  </dependency>

  <!-- JAXB Implementation -->
  <dependency>
      <groupId>com.sun.xml.bind</groupId>
      <artifactId>jaxb-impl</artifactId>
      <version>3.0.0</version>
      <scope>runtime</scope>
  </dependency>

The same APIs in the jakarta.xml.* package.


// Jakarta EE packages
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Marshaller;

7. Download Source Code

$ git clone https://github.com/mkyong/core-java

$ cd java-xml

$ cd src/main/java/com/mkyong/xml/jaxb/

8. References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
3 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Andres
9 months ago

First time, in years, I do not understand a post from you !

Vijay
8 days ago

Hi All, I am getting unexpected option -b when i am using apache cxf 4.0.4 version for generating classes from wsdl. i am using wsdl2java command and passing wsdl and -b as option to send external bindings .xjb .xml file paths as a seperate -b options. it was working fine in earlier versions of java like java 11 but not in java 17 (jakarta). any clue on this why it is giving this error. i tried by changing name spaces to jakarta in .xjb also

Roy
3 months ago

Thank you!