ClassCastException: attempting to castbundleresource on javax.ws.rs

I have run into a classcastexception that I am unable to solve. In the helios binding we use javax.ws.rs.Client, which is provided by the runtime (cfr output from “bundle:capabilities”):

javax.ws.rs-api_2.0.1 [35] provides:

osgi.wiring.package; javax.ws.rs.client 2.0.1 required by:
org.glassfish.jersey.core.jersey-server_2.22.2 [126]
org.glassfish.jersey.core.jersey-client_2.22.2 [124]
org.glassfish.jersey.core.jersey-common_2.22.2 [125]
org.glassfish.jersey.media.jersey-media-sse_2.22.2 [128]
org.openhab.binding.helios_2.5.10.202010140401 [287]

However, when the helios bundle is running, and when a call is made to javax.ws.rs.client.ClientBuilder(), I do get the following error

Exception occurred while initializing handler of thing 'helios:ipvario221:gate': ClassCastException: attempting to castbundleresource://35.fwk1058650291/javax/ws/rs/client/ClientBuilder.class to bundleresource://35.fwk1058650291/javax/ws/rs/client/ClientBuilder.class
java.lang.LinkageError: ClassCastException: attempting to castbundleresource://35.fwk1058650291/javax/ws/rs/client/ClientBuilder.class to bundleresource://35.fwk1058650291/javax/ws/rs/client/ClientBuilder.class
at javax.ws.rs.client.ClientBuilder.newBuilder(ClientBuilder.java:97) ~[?:?]
at org.openhab.binding.helios.internal.handler.HeliosHandler221.initialize(HeliosHandler221.java:173) ~[?:?]
at jdk.internal.reflect.GeneratedMethodAccessor102.invoke(Unknown Source) ~[?:?]
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
at java.lang.reflect.Method.invoke(Method.java:566) ~[?:?]
at org.eclipse.smarthome.core.internal.common.AbstractInvocationHandler.invokeDirect(AbstractInvocationHandler.java:152) [bundleFile:?]
at org.eclipse.smarthome.core.internal.common.Invocation.call(Invocation.java:52) [bundleFile:?]
at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
at java.lang.Thread.run(Thread.java:834) [?:?]

What surprises me is that class being referenced (35.fwk1058650291) is the same. If it would be different then I could imagine that another bundle is exporting a javax.ws.rs provider, but is not the case

When I beef up the POM and feature.xml to include Glassfish directly, e.g.
>

    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>2.22.2</version>
 </dependency>

and

<bundle dependency= *"true"* >mvn:org.glassfish.jersey.core/jersey-client/2.22.2</bundle>

then this does not change anything. In fact, the POM and feature are “equal”, moreover, the error only happens in Karaf, and not in Eclipse.

Any help is appreciated

As a follow-up question, why does maven generate a feature.xml that looks like

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<features xmlns="http://karaf.apache.org/xmlns/features/v1.6.0" name="org.openhab.binding.helios-2.5.10-SNAPSHOT">
    <repository>mvn:org.openhab.core.features.karaf/org.openhab.core.features.karaf.openhab-core/2.5.0/xml/features</repository>
    <feature name="openhab-binding-helios" description="Helios Binding" version="2.5.10.SNAPSHOT">
        <feature>openhab-runtime-base</feature>
        <bundle dependency="true">mvn:org.apache.ws.xmlschema/xmlschema-core/2.2.4</bundle>
        <bundle dependency="true">mvn:org.apache.servicemix.specs/org.apache.servicemix.specs.jaxws-api-2.2/2.9.0</bundle>
        <bundle dependency="true">mvn:org.apache.servicemix.specs/org.apache.servicemix.specs.saaj-api-1.3/2.9.0</bundle>
        <bundle dependency="true">mvn:org.apache.cxf/cxf-core/3.1.14</bundle>
        <bundle dependency="true">mvn:org.apache.cxf.services.wsn/cxf-services-wsn-api/3.1.14</bundle>
        <bundle dependency="true">mvn:org.apache.felix/org.apache.felix.framework/6.0.2</bundle>
        <bundle dependency="true">mvn:org.apache.geronimo.specs/geronimo-ws-metadata_2.0_spec/1.1.3</bundle>
        <bundle dependency="true">mvn:org.glassfish.jersey.core/jersey-client/2.22.2</bundle>
        <bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.helios/2.5.10-SNAPSHOT</bundle>
    </feature>
    <feature name="org.openhab.binding.helios" description="openHAB Add-ons :: Bundles :: Helios Binding" version="2.5.10.SNAPSHOT">
        <details>This project contains the official add-ons of openHAB</details>
        <bundle start-level="80">mvn:org.glassfish.jersey.core/jersey-client/2.22.2</bundle>
        <bundle start-level="80">mvn:javax.ws.rs/javax.ws.rs-api/2.0.1</bundle>
        <bundle start-level="80">mvn:org.glassfish.jersey.core/jersey-common/2.22.2</bundle>
        <bundle start-level="80">mvn:org.glassfish.jersey.bundles.repackaged/jersey-guava/2.22.2</bundle>
        <bundle start-level="80">mvn:org.glassfish.hk2/osgi-resource-locator/1.0.1</bundle>
        <bundle start-level="80">mvn:org.glassfish.hk2/hk2-api/2.4.0-b34</bundle>
        <bundle start-level="80">mvn:org.glassfish.hk2/hk2-utils/2.4.0-b34</bundle>
        <bundle start-level="80">mvn:org.glassfish.hk2.external/aopalliance-repackaged/2.4.0-b34</bundle>
        <bundle start-level="80">mvn:org.glassfish.hk2.external/javax.inject/2.4.0-b34</bundle>
        <bundle start-level="80">mvn:org.glassfish.hk2/hk2-locator/2.4.0-b34</bundle>
        <bundle start-level="80">mvn:org.javassist/javassist/3.18.1-GA</bundle>
        <bundle start-level="80">mvn:org.apache.geronimo.specs/geronimo-ws-metadata_2.0_spec/1.1.3</bundle>
        <bundle start-level="80">mvn:org.apache.ws.xmlschema/xmlschema-core/2.2.4</bundle>
        <bundle start-level="80">mvn:org.apache.cxf/cxf-core/3.1.14</bundle>
        <bundle start-level="80">mvn:org.codehaus.woodstox/woodstox-core-asl/4.4.1</bundle>
        <bundle start-level="80">mvn:org.codehaus.woodstox/stax2-api/3.1.4</bundle>
        <bundle start-level="80">mvn:org.apache.cxf.services.wsn/cxf-services-wsn-api/3.1.14</bundle>
    </feature>
</features>

e.g. for other bindings, when compiling, I only get the upper section (the one basically derived from the feature.xml in the source tree/folder)

For anyone encountering the same problem, it is linked to the kar cache being rebuilt (after being wiped out), and so, in my case the problem went away “automagically”