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?