xml serialization - any experience which plugins/packages might work

Hi,
as there are security issues, I was looking for a solution to get rid of my xstream dependency. I played around with the Jakarta XML binding library. In the KNIME target platform is the plugin jakarta.xml.bind available as version 2.3.3.
But I get ClassNotFoundException during runtime.

This is the piece of code and the error it throws:

JAXBContext jaxbContext = JAXBContext.newInstance(ScriptTemplate.class);
WARN 	 main WrappedNodeDialog	 failed to apply settings: 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]
org.knime.core.node.InvalidSettingsException: 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 de.mpicbg.knime.scripting.core.ScriptingNodeDialog.saveAdditionalSettingsTo(ScriptingNodeDialog.java:462)
	at org.knime.core.node.defaultnodesettings.DefaultNodeSettingsPane.saveSettingsTo(DefaultNodeSettingsPane.java:293)
	at org.knime.core.node.NodeDialogPane.internalSaveSettingsTo(NodeDialogPane.java:540)
	at org.knime.core.node.NodeDialogPane$4.run(NodeDialogPane.java:779)
	at org.knime.core.node.util.ViewUtils$3.run(ViewUtils.java:353)
	at org.knime.core.node.util.ViewUtils$2.run(ViewUtils.java:155)
	at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:318)
	at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:771)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
	at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:741)
	at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Caused by: 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:255)
	at jakarta.xml.bind.ContextFinder.newInstance(ContextFinder.java:243)
	at jakarta.xml.bind.ContextFinder.find(ContextFinder.java:407)
	at jakarta.xml.bind.JAXBContext.newInstance(JAXBContext.java:691)
	at jakarta.xml.bind.JAXBContext.newInstance(JAXBContext.java:632)
	at de.mpicbg.knime.scripting.core.rgg.wizard.ScriptTemplate.toXML(ScriptTemplate.java:178)
	at de.mpicbg.knime.scripting.core.ScriptingNodeDialog.saveAdditionalSettingsTo(ScriptingNodeDialog.java:460)
	... 18 more
Caused by: java.lang.ClassNotFoundException: org.glassfish.jaxb.runtime.v2.ContextFactory
	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 jakarta.xml.bind.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:92)
	at jakarta.xml.bind.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:125)
	at jakarta.xml.bind.ContextFinder.newInstance(ContextFinder.java:253)
	... 24 more

Then, I tried to make use of the library (version 3.0.1) via my plugin libraries. I tried to download dependencies but failed.

I was wondering whether I can get a helpful hint here how to get things working. Maybe there are already experiences. I can also use a different library. I just need some way to serialize a class as XML.

Best,
Antje

Hi @niederle,

These kinds of JAXBContext initialization is a bit tricky in OSGi. You need to set the context classloader to be the same as one of the classes from your plugin.

best,
Gabriel

1 Like

Hi Gabriel,

thanks a lot for your answer.
I even tried that like:

    	ClassLoader currentLoader = Thread.currentThread().getContextClassLoader();
		Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
		
    	JAXBContext jaxbContext = JAXBContext.newInstance(ScriptTemplate.class);
    	
    	Thread.currentThread().setContextClassLoader(currentLoader);

but then the error message just changes to:

WARN 	 main WrappedNodeDialog	 failed to apply settings: 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 cannot be found by de.mpicbg.knime.scripting.core_4.2.0.qualifier]
org.knime.core.node.InvalidSettingsException: 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 cannot be found by de.mpicbg.knime.scripting.core_4.2.0.qualifier]
	at de.mpicbg.knime.scripting.core.ScriptingNodeDialog.saveAdditionalSettingsTo(ScriptingNodeDialog.java:462)
	at org.knime.core.node.defaultnodesettings.DefaultNodeSettingsPane.saveSettingsTo(DefaultNodeSettingsPane.java:293)
	at org.knime.core.node.NodeDialogPane.internalSaveSettingsTo(NodeDialogPane.java:540)
	at org.knime.core.node.NodeDialogPane$4.run(NodeDialogPane.java:779)
	at org.knime.core.node.util.ViewUtils$3.run(ViewUtils.java:353)
	at org.knime.core.node.util.ViewUtils$2.run(ViewUtils.java:155)
	at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:318)
	at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:771)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
	at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:741)
	at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Caused by: 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 cannot be found by de.mpicbg.knime.scripting.core_4.2.0.qualifier]
	at jakarta.xml.bind.ContextFinder.newInstance(ContextFinder.java:255)
	at jakarta.xml.bind.ContextFinder.newInstance(ContextFinder.java:243)
	at jakarta.xml.bind.ContextFinder.find(ContextFinder.java:407)
	at jakarta.xml.bind.JAXBContext.newInstance(JAXBContext.java:691)
	at jakarta.xml.bind.JAXBContext.newInstance(JAXBContext.java:632)
	at de.mpicbg.knime.scripting.core.rgg.wizard.ScriptTemplate.toXML(ScriptTemplate.java:178)
	at de.mpicbg.knime.scripting.core.ScriptingNodeDialog.saveAdditionalSettingsTo(ScriptingNodeDialog.java:460)
	... 18 more
Caused by: java.lang.ClassNotFoundException: org.glassfish.jaxb.runtime.v2.ContextFactory cannot be found by de.mpicbg.knime.scripting.core_4.2.0.qualifier
	at org.eclipse.osgi.internal.loader.BundleLoader.generateException(BundleLoader.java:541)
	at org.eclipse.osgi.internal.loader.BundleLoader.findClass0(BundleLoader.java:536)
	at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:416)
	at org.eclipse.osgi.internal.loader.ModuleClassLoader.loadClass(ModuleClassLoader.java:168)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
	at jakarta.xml.bind.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:92)
	at jakarta.xml.bind.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:125)
	at jakarta.xml.bind.ContextFinder.newInstance(ContextFinder.java:253)
	... 24 more

Is there anything, I missed?

Best,
Antje

1 Like

Thank’s a lot for troubleshooting this issue, @gab1one !

The following setup made it work:

  1. adding these two plugins as dependencies (MANIFEST.MF:
Require-Bundle: com.sun.xml.bind;bundle-version="2.3.3",
 jakarta.xml.bind;bundle-version="2.3.3",
  1. within the META-INF folder a subfolder named services had to be created.
  2. within that services folder a file named javax.xml.bind.JAXBContext has been created with the following content:
com.sun.xml.bind.v2.ContextFactory
  1. The context class loader had to be set to the one of my plugin to make the JAXBContext.newInstance(...) method work
ClassLoader currentLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
		
JAXBContext jaxbContext = JAXBContext.newInstance(ScriptTemplate.class);
    	
Thread.currentThread().setContextClassLoader(currentLoader);

Best,
Antje

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.