You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Relates to #99
(maybe even the same as that issue, but the issue still occurs with the latest jaxb-api version)
Version
jakarta.xml.bind:jakarta.xml.bind-api:4.0.2
org.glassfish.jaxb:jaxb-runtime:4.0.5
JDK 21
Description
JAXB's ContextFinder is unable to find the JAXBContext implementation when the thread context class loader contains none of the JAXB jars. JAXBContext.newInstance fails with:
Exception in thread "main" jakarta.xml.bind.JAXBException: Implementation of Jakarta XML Binding-API has not been found on module path or classpath.
- with linked exception:
[java.lang.ClassNotFoundException: org.glassfish.jaxb.runtime.v2.ContextFactory]
at jakarta.xml.bind.ContextFinder.newInstance(ContextFinder.java:250)
at jakarta.xml.bind.ContextFinder.newInstance(ContextFinder.java:238)
at jakarta.xml.bind.ContextFinder.find(ContextFinder.java:386)
at jakarta.xml.bind.JAXBContext.newInstance(JAXBContext.java:605)
at jakarta.xml.bind.JAXBContext.newInstance(JAXBContext.java:546)
at org.example.Main.main(Main.java:17)
Caused by: java.lang.ClassNotFoundException: org.glassfish.jaxb.runtime.v2.ContextFactory
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:445)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:593)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
at jakarta.xml.bind.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:113)
at jakarta.xml.bind.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:146)
at jakarta.xml.bind.ContextFinder.newInstance(ContextFinder.java:248)
... 5 more
The problem might be that jakarta.xml.bind.ServiceLoaderUtil uses only ServiceLoader.load(Class), which implicitly uses the thread context class loader. Maybe it should fall back to using ServiceLoader.load(Class, ClassLoader) (and use ServiceLoaderUtil.class.getClassLoader())? But I am not sure if that could have any undesired implications.
Then run Main.main; it will fail with the JAXBException mentioned above.
(this is a bit contrived example, but I hope it matches the actual use case described in the "Additional context" section below close enough)
Additional context
I am currently experiencing this while working on a plugin for Jenkins. As described in the Jenkins documentation the context class loader normally does not contain the plugin classes (and therefore not the JAXB classes used by the plugin).
It seems many Jenkins plugins have worked around this by temporarily changing the context class loader, see for example https://issues.jenkins.io/browse/JENKINS-68514. Most of them have done that for the old javax.xml.bind package, but it probably applies to the new jakarta.xml.bind package name as well. At least for the plugin I am working on that seems to be the case.
The text was updated successfully, but these errors were encountered:
Yes, being able to specify the ClassLoader to use would probably help (unless JAXBContext is used in third-party code you cannot edit). But it would of course be even better if it just worked automatically without having to explicitly specify a ClassLoader, in case that is possible.
Relates to #99
(maybe even the same as that issue, but the issue still occurs with the latest jaxb-api version)
Version
jakarta.xml.bind:jakarta.xml.bind-api:4.0.2
org.glassfish.jaxb:jaxb-runtime:4.0.5
JDK 21
Description
JAXB's
ContextFinder
is unable to find theJAXBContext
implementation when the thread context class loader contains none of the JAXB jars.JAXBContext.newInstance
fails with:The problem might be that
jakarta.xml.bind.ServiceLoaderUtil
uses onlyServiceLoader.load(Class)
, which implicitly uses the thread context class loader. Maybe it should fall back to usingServiceLoader.load(Class, ClassLoader)
(and useServiceLoaderUtil.class.getClassLoader()
)? But I am not sure if that could have any undesired implications.Reproduction steps
Create a small Maven project:
/pom.xml
/src/main/java/org/example/Main.java
Then run
Main.main
; it will fail with theJAXBException
mentioned above.(this is a bit contrived example, but I hope it matches the actual use case described in the "Additional context" section below close enough)
Additional context
I am currently experiencing this while working on a plugin for Jenkins. As described in the Jenkins documentation the context class loader normally does not contain the plugin classes (and therefore not the JAXB classes used by the plugin).
It seems many Jenkins plugins have worked around this by temporarily changing the context class loader, see for example https://issues.jenkins.io/browse/JENKINS-68514. Most of them have done that for the old
javax.xml.bind
package, but it probably applies to the newjakarta.xml.bind
package name as well. At least for the plugin I am working on that seems to be the case.The text was updated successfully, but these errors were encountered: