7

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.
  • with linked exception:
 [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

Rishabh Ohri
  • 1,280
  • 4
  • 16
  • 28
  • Got this working? Struggling with the same behavior in my application... – Origin Apr 26 '21 at 15:58
  • 1
    Does this answer your question? [Replacements for deprecated JPMS modules with Java EE APIs](https://stackoverflow.com/questions/48204141/replacements-for-deprecated-jpms-modules-with-java-ee-apis) – 9ilsdx 9rvj 0lo Jun 07 '21 at 11:54

2 Answers2

0

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>
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
  • I am still facing the issue - Caused by: javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath. With the following dependencies in my POM - – Rishabh Ohri Nov 13 '20 at 13:53
  • I am also seeing that when I build the library in Java I dont see any of the dependencies in the external libraries. Could that be a problem and I have to manually bundle the JAXB libraries. – Rishabh Ohri Nov 13 '20 at 13:58
  • 1
    Dependency to activation-api version 1.2.1 (not 2.0.0) is already added to the jaxb-runtime:2.3.2. See https://mvnrepository.com/artifact/org.glassfish.jaxb/jaxb-runtime/2.3.2. – Emil Sierżęga Mar 23 '21 at 18:15
0

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>