Rishabh Ohri
Rishabh Ohri

Reputation: 1310

Java 8 to Java 11 migration issues with JAXB API

We are in process of migrating our projects from Java 8 to Java 11. One of the APIs is dependent on a library that utilizes JAXB. As we know JAXB was removed from JAVA 11 we started to include the JAXB dependencies in the POM of the library.

 <!-- https://mvnrepository.com/artifact/jakarta.xml.bind/jakarta.xml.bind-api -->
  <!-- API -->
  <dependency>
    <groupId>jakarta.xml.bind</groupId>
    <artifactId>jakarta.xml.bind-api</artifactId>
    <version>2.3.3</version>
  </dependency>
  <!-- Runtime -->
  <dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>2.3.2</version>
  </dependency>

All works fine when we run the API project with

mvn spring-boot:run

However when the API is deployed in QA servers and is started using a start script with -

java -jar Sample-api-3.0.0-SNAPSHOT.jar

The API throws the following error when invoking the library that is dependent on JAXB -

Caused by: javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
 [java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory]
      at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:269)
      at javax.xml.bind.ContextFinder.find(ContextFinder.java:412)
      at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:721)
      at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:662)
      at com.eoriginal.ecore.client.documents.SearchDocumentHistoryAPI$RequestParameters.toMap(SearchDocumentHistoryAPI.java:344)
      ... 14 more
    Caused by: java.lang.ClassNotFoundException: com.sun.xml.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:267)

UPDATE : I added the Maven Shade plugin to generate the JAR with all the dependencies but when the line of code is executed to create the JAXBContext the error still persists - JAXBContext jaxbc = JAXBContext.newInstance(new Class[]{Abc.class}); enter image description here

Upvotes: 7

Views: 17037

Answers (2)

Ali Karimi
Ali Karimi

Reputation: 5

Add this dependency to POM:

        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>

Upvotes: 0

Nikolas
Nikolas

Reputation: 44476

JAXB needs javax.activation module, which became jakarta.activation after rebranding to JakartaEE. Inlcuding this dependency should help:

<dependency>
    <groupId>jakarta.activation</groupId>
    <artifactId>jakarta.activation-api</artifactId>
    <version>2.0.0</version>
</dependency>

Upvotes: 0

Related Questions