JAXB-runtime lib in IntelliJ Plugin

Answered

Hello,

I'm struggling with the use of JAXB in my IntelliJ plugin. The short version: I think my problem is how to properly include a runtime-only library in my plugin.


In more detail:

In the instantiation of my plugin, I want to parse one or more XML files. So I generated classes from an xsd using the IntelliJ tool, which uses xjc.

I added the neccessary gradle dependencies 
implementation("jakarta.xml.bind:jakarta.xml.bind-api:4.0.0")
implementation("org.glassfish.jaxb:jaxb-runtime:4.0.0")


I know that the second dep is neccessary, as a runtime is needed. But it is not found at runtime. I get the error

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]

By debugging, I see that the class loader does not find an implementation. 
The jaxb-runtime.lib is present in my idea-sandbox/plugins/my-plugin/lib directory, among the other libs.


When I extract all parts into a tiny java program, everything works. The runtime lib is found. So this seems to be a difference in the class loader handling in IntelliJ plugins.


Can someone explain me why in case of a plugin the runtime-lib is not found?

Thank you very much!

0
3 comments

I found the reason: JAXB loads the implementation at runtime - “For this to work in a plugin, the context class loader must be set to the plugin's classloader and restored afterwards with the original one around initialization code”.

https://plugins.jetbrains.com/docs/intellij/plugin-class-loaders.html#using-serviceloader

 

Now it works.

0

Rejoiced too soon. This solution only works in local configuration, but not after installing the plugin.

 

Any thoughts would be appreciated.

0

For those who struggle with this problem, here's my solution:

I still generate JAXB classes using Intellij, which uses xjc. Afterwards, in the generated sources I replace all “import jakarta.xml” with “import javax.xml”.

In gradle, I use “Old JAXB Runtime” (implementation("com.sun.xml.bind:jaxb-impl:4.0.5")). It is still under support and works like a charm.

 

0

Please sign in to leave a comment.