0

I'm trying trying to load Hibernate XML configuration on OpenJ9 (JDK14) but since JAXB are not longer part of the JDK I get this error:

Caused by: java.lang.ClassNotFoundException: javax.xml.bind.ValidationEventHandler

I tried to follow some of the answers on: How to resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException in Java 9 but it seems like none are working on OpenJ9 and when trying to include any of the dependencies I'm getting the following error:

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.internal.bind.v2.ContextFactory]
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:278)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:421)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:721)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:662)
    at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:122)
    ... 5 more
Caused by: java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:788)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:1081)
    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:276)
    ... 9 more


Guy Korland
  • 9,139
  • 14
  • 59
  • 106

1 Answers1

0

I found the missing part in the puzzle, the javax.xml.bind.JAXBContextFactory needs to be set in the system property.

So the solution is adding JAXB to list of dependencies:

        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.moxy</artifactId>
            <version>2.7.3</version>
        </dependency>

And then in the code (or when the JVM starts) set the system property to point to the right factory.

    System.setProperty("javax.xml.bind.JAXBContextFactory", "org.eclipse.persistence.jaxb.JAXBContextFactory");

Last since I'm running in a custom classloader the ContextClassLoader has to be set otherwise the factory can't be found.

Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
Guy Korland
  • 9,139
  • 14
  • 59
  • 106