Apache Karaf 4.3.7 JAXB jakarta.xml.bind.JAXBException: Implementation of Jakarta XML Binding-API has not been found on module path or classpath

Viewed 15

I want to marshal unmarshal using JAXB from my bundle deployed on Karaf 4.3.7, OpenJDK 17. It works in IDE in unit test but not in Karaf.

I receive this exception in Karaf:

jakarta.xml.bind.JAXBException: Implementation of Jakarta XML Binding-API has not been found on module path or classpath. at jakarta.xml.bind.ContextFinder.newInstance(ContextFinder.java:252) ~[!/:4.0.0] at jakarta.xml.bind.ContextFinder.newInstance(ContextFinder.java:240) ~[!/:4.0.0] at jakarta.xml.bind.ContextFinder.find(ContextFinder.java:381) ~[!/:4.0.0] at jakarta.xml.bind.JAXBContext.newInstance(JAXBContext.java:605) ~[!/:4.0.0] at jakarta.xml.bind.JAXBContext.newInstance(JAXBContext.java:546) ~[!/:4.0.0]

I tried one by one multiple JAXB implementations on Karaf:

           <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.moxy</artifactId>
            <version>4.0.0-RC2</version>
        </dependency>
        <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
            <version>4.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>4.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>4.0.0</version>
        </dependency>

I also tried jaxb.properties for Moxy:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory jakarta.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory jakarta.xml.bind.JAXBContextFactory=org.eclipse.persistence.jaxb.JAXBContextFactory

I also tried with installed Karaf features:

jaxb cxf-jaxb

But the implementation is never found when using:

JAXBContext context = JAXBContext.newInstance(classes); JAXBContext context = JAXBContext.newInstance(clazz.getPackageName(), clazz.getClassLoader());

The only thing which worked, but not in all cases, for example it fails for @XmlEnum, is:

JAXBContext context = org.eclipse.persistence.jaxb.JAXBContextFactory .createContext(classes, null);

I checked various posts but they usually don't write about the OSGI environment. They simply add a JAXB implementation on Java 11 or higher and it works.

What am I missing?

1 Answers

Finally I made it work with:

Maven:

 <dependency>
   <groupId>org.eclipse.persistence</groupId>
   <artifactId>org.eclipse.persistence.moxy</artifactId>
   <version>4.0.0-RC2</version>
 </dependency>

Karaf feature:

<bundle>mvn:org.eclipse.persistence/org.eclipse.persistence.asm/9.3.0</bundle>
<bundle>mvn:org.eclipse.persistence/org.eclipse.persistence.core/4.0.0-RC2</bundle>
<bundle>mvn:org.eclipse.persistence/org.eclipse.persistence.moxy/4.0.0-RC2</bundle>
<bundle>mvn:jakarta.xml.bind/jakarta.xml.bind-api/4.0.0</bundle>

In Java:

JAXBContext context = org.eclipse.persistence.jaxb.JAXBContextFactory
            .createContext(classes, null);

My missing part was to import explicitly the eclipse persistence packages to all my bundles that use JAXB api, bnd maven plugin:

Import-Package: org.eclipse.persistence.internal.jaxb.json.schema.model,   \
            org.eclipse.persistence.internal.jaxb.json.schema,         \
            org.eclipse.persistence.internal.jaxb.many,                \
            org.eclipse.persistence.internal.jaxb,                     \
            org.eclipse.persistence.jaxb.attachment,                   \
            org.eclipse.persistence.jaxb.compiler.builder.helper,      \
            org.eclipse.persistence.jaxb.compiler.builder,             \
            org.eclipse.persistence.jaxb.compiler.facets,              \
            org.eclipse.persistence.jaxb.compiler,                     \
            org.eclipse.persistence.jaxb.dynamic.metadata,             \
            org.eclipse.persistence.jaxb.dynamic,                      \
            org.eclipse.persistence.jaxb.javamodel.oxm,                \
            org.eclipse.persistence.jaxb.javamodel.reflection,         \
            org.eclipse.persistence.jaxb.javamodel.xjc,                \
            org.eclipse.persistence.jaxb.javamodel,                    \
            org.eclipse.persistence.jaxb.json,                         \
            org.eclipse.persistence.jaxb.metadata,                     \
            org.eclipse.persistence.jaxb.plugins,                      \
            org.eclipse.persistence.jaxb.rs,                           \
            org.eclipse.persistence.jaxb.xmlmodel,                     \
            org.eclipse.persistence.jaxb,*
Related