'Using JAX-B in Eclipse 4.23 Plugins

I need to update my Eclipse plugin to Java 17 and Eclipse 4.23 (2022-03). The plugin used JAX-B in Java 8 which included the required classes, but now i need to change it to separate libraries.

In order to do so i added these to the list of "Required Plug-ins"

jakarta.xml.bind
org.eclipse.persistence.moxy

Both plugins are readily available in my installation, so that seemed to be the most sensible approach.

It seems that the javax.xml.bind.ContextFinder included in jakarta.xml.bind is found and tries to load org.eclipse.persistence.jaxb.JAXBContextFactory. But that step fails at JAXBContext.newInstance:

javax.xml.bind.JAXBException: Implementation of JAXB-API has not been
found on module path or classpath.
- with linked exception: [java.lang.ClassNotFoundException:
org.eclipse.persistence.jaxb.JAXBContextFactory]
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:232)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:345)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:691)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:632)

Caused by: java.lang.ClassNotFoundException: org.eclipse.persistence.jaxb.JAXBContextFactory
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
    at org.eclipse.osgi.internal.framework.ContextFinder.loadClass(ContextFinder.java:147)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
    at javax.xml.bind.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:92)
    at javax.xml.bind.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:125)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:230)
    ... 46 more

What confuses me greatly about this is that the class JAXBContextFactory can be found if i use JAXBContext.class.getClassLoader().getResource("org/eclipse/persistence/jaxb/JAXBContextFactory.class") where the class loader is org.eclipse.osgi.internal.loader.EquinoxClassLoader. But the internal workings of JAXBContext appear to use a different class loader (org.eclipse.osgi.internal.framework.ContextFinder) and that can't find it.

How can i get it to work?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source