OSGi dependency hell: ClassNotFoundException: org.eclipse.persistence.internal.jaxb.WrappedValue

I had working environment with Equinox, Jersey and MOXy. Jersey uses MOXy to parse JSON and bind to bean objects. After changing one field type of some @XmlRootElement class to enum, I get:

java.lang.NoClassDefFoundError: org/eclipse/persistence/internal/jaxb/WrappedValue
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.lang.ClassLoader.defineClass(ClassLoader.java:642)
at org.eclipse.persistence.internal.jaxb.JaxbClassLoader.generateClass(JaxbClassLoader.java:124)
at org.eclipse.persistence.jaxb.compiler.MappingsGenerator.generateWrapperClass(MappingsGenerator.java:3232)
...

MOXy bundle seems to be fine:

osgi> lb -l
...
50|Active     |    4|initial@reference:file:plugins/org.eclipse.persistence.moxy_2.6.0.v20150309-bf26070.jar
...

There is the class in the jar

$ jar tf plugins/org.eclipse.persistence.moxy_2.6.0.v20150309-bf26070.jar | grep Wrapped
org/eclipse/persistence/internal/jaxb/WrappedValue.class

and package is exported

osgi> bundle 50
...
Exported packages
  org.eclipse.persistence.internal.jaxb; version="2.6.0"[exported]
...

so why do I get ClassNotFoundException?

EDIT: bundes:

0|Active     |    0|System Bundle
1|Active     |    4|initial@reference:file:plugins/org.mongodb.morphia_0.110.0.jar
2|Active     |    4|initial@reference:file:plugins/javax.validation.api_1.1.0.Final.jar
3|Active     |    4|initial@reference:file:plugins/classmate_1.0.0.jar
4|Active     |    4|initial@reference:file:plugins/org.hibernate.validator_5.1.3.Final.jar
5|Active     |    4|initial@reference:file:plugins/commons-logging_1.1.1.jar
6|Active     |    4|initial@reference:file:plugins/org.eclipse.jetty.server_8.1.16.v20140903.jar
7|Active     |    4|initial@reference:file:plugins/com.thoughtworks.proxytoys_1.0.0.jar
8|Active     |    4|initial@reference:file:plugins/org.eclipse.jetty.http_8.1.16.v20140903.jar
9|Active     |    4|initial@reference:file:plugins/org.eclipse.equinox.http.jetty_3.0.200.v20131021-1843.jar
10|Active     |    4|initial@reference:file:../../../../edu.zut.aci.nsg.restapi/
11|Active     |    4|initial@reference:file:plugins/org.mongodb.morphia.util_0.110.0.jar
12|Active     |    4|initial@reference:file:plugins/org.eclipse.osgi.services_3.4.0.v20140312-2051.jar
13|Active     |    4|initial@reference:file:plugins/org.eclipse.equinox.http.servlet_1.1.500.v20140318-1755.jar
14|Active     |    4|initial@reference:file:plugins/org.glassfish.javax.json_1.0.4.jar
15|Active     |    4|initial@reference:file:plugins/org.glassfish.jersey.ext.jersey-bean-validation_2.17.0.jar
16|Active     |    4|initial@reference:file:plugins/org.glassfish.jersey.media.jersey-media-jaxb_2.17.0.jar
17|Active     |    4|initial@reference:file:plugins/org.glassfish.hk2.utils_2.4.0.b10.jar
18|Active     |    4|initial@reference:file:plugins/org.glassfish.jersey.containers.jersey-container-servlet_2.17.0.jar
19|Active     |    4|initial@reference:file:plugins/org.glassfish.jersey.core.jersey-client_2.17.0.jar
20|Active     |    4|initial@reference:file:plugins/org.eclipse.persistence.core_2.6.0.v20150309-bf26070.jar
21|Active     |    4|initial@reference:file:plugins/javassist_3.18.1.GA.jar
22|Active     |    4|initial@reference:file:plugins/org.eclipse.jetty.security_8.1.16.v20140903.jar
23|Active     |    4|initial@reference:file:plugins/org.glassfish.hk2.external.javax.inject_2.4.0.b10.jar
24|Active     |    4|initial@reference:file:plugins/com.eclipsesource.jaxrs.publisher_4.3.0.201503290743.jar
25|Active     |    4|initial@reference:file:plugins/org.mongodb.mongo-java-driver_2.13.0.RELEASE.jar
26|Active     |    4|initial@reference:file:plugins/org.eclipse.persistence.asm_5.0.1.v201405080102.jar
27|Active     |    4|initial@reference:file:plugins/com.google.gson_2.3.1.jar
28|Active     |    4|initial@reference:file:plugins/org.eclipse.jetty.util_8.1.16.v20140903.jar
29|Active     |    4|initial@reference:file:plugins/org.eclipse.jetty.servlet_8.1.16.v20140903.jar
30|Active     |    4|initial@reference:file:plugins/org.eclipse.jetty.io_8.1.16.v20140903.jar
31|Active     |    1|initial@reference:file:plugins/org.eclipse.equinox.ds_1.4.200.v20131126-2331.jar
32|Active     |    4|initial@reference:file:plugins/org.glassfish.hk2.api_2.4.0.b10.jar
33|Active     |    4|initial@reference:file:plugins/org.apache.felix.gogo.runtime_0.10.0.v201209301036.jar
34|Active     |    4|initial@reference:file:plugins/org.eclipse.jetty.continuation_8.1.16.v20140903.jar
35|Active     |    4|initial@reference:file:plugins/org.eclipse.equinox.console_1.1.0.v20140131-1639.jar
36|Active     |    4|initial@reference:file:plugins/com.eclipsesource.jaxrs.provider.security_2.1.0.201503290743.jar
37|Active     |    4|initial@reference:file:plugins/javax.el_2.2.0.v201303151357.jar
38|Active     |    4|initial@reference:file:plugins/javax.ws.rs-api_2.0.1.jar
39|Active     |    4|initial@reference:file:plugins/org.glassfish.hk2.osgi-resource-locator_1.0.1.jar
40|Active     |    4|initial@reference:file:plugins/org.glassfish.jersey.bundles.repackaged.jersey-guava_2.17.0.jar
41|Active     |    4|initial@reference:file:plugins/org.jboss.logging.jboss-logging_3.1.3.GA.jar
42|Active     |    4|initial@reference:file:plugins/org.eclipse.equinox.util_1.0.500.v20130404-1337.jar
43|Active     |    4|initial@reference:file:plugins/org.glassfish.hk2.external.aopalliance-repackaged_2.4.0.b10.jar
44|Active     |    4|initial@reference:file:plugins/javax.servlet-api_3.0.1.jar
45|Active     |    4|initial@reference:file:plugins/org.apache.felix.gogo.shell_0.10.0.v201212101605.jar
46|Active     |    4|initial@reference:file:plugins/org.glassfish.jersey.core.jersey-common_2.17.0.jar
47|Active     |    4|initial@reference:file:plugins/org.glassfish.hk2.locator_2.4.0.b10.jar
48|Active     |    4|initial@reference:file:plugins/javax.annotation-api_1.2.0.jar
49|Active     |    4|initial@reference:file:plugins/org.apache.commons.configuration_1.10.0.jar
50|Active     |    4|initial@reference:file:plugins/org.eclipse.persistence.moxy_2.6.0.v20150309-bf26070.jar
51|Active     |    4|initial@reference:file:plugins/org.scala-lang.scala-library_2.11.6.v20150224-172222-092690e7bf.jar
52|Active     |    4|initial@reference:file:plugins/org.apache.commons.lang_2.6.0.jar
53|Active     |    4|initial@reference:file:plugins/org.glassfish.web.javax.el_2.2.4.jar
54|Active     |    4|initial@reference:file:plugins/org.slf4j.api_1.7.2.v20121108-1250.jar
55|Active     |    4|initial@reference:file:plugins/org.glassfish.jersey.containers.jersey-container-servlet-core_2.17.0.jar
56|Active     |    4|initial@reference:file:plugins/org.apache.felix.gogo.command_0.10.0.v201209301215.jar
57|Active     |    4|initial@reference:file:plugins/org.glassfish.jersey.core.jersey-server_2.17.0.jar

Change in data model which causes the problem:

from:

@XmlRootElement
public class Role implements Serializable {

    public static final String COMMON = "common";
    public static final String REGISTRANT = "registrant";
    ...

to:

@XmlEnum(String.class)
public enum Role {

    @XmlEnumValue("common") COMMON ("common"),
    @XmlEnumValue("registrant") REGISTRANT ("registrant"),
    @XmlEnumValue("laborant") LABORANT ("laborant"),
    ...

Usage looks like:

@XmlRootElement
public class User implements Principal, Serializable {
    private static final long serialVersionUID = 1L;
    ...
    private List<Role> roles = new ArrayList<Role>();
    ...

and

@PUT @Path("/{name}")
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
public Response register(@PathParam("name") String name, @Valid User user) {
    ...

I belive it may be caused by a missing entry in the Import-Package manifest header in the bundle that uses JAXB. I had a similar stacktrace and when I added org.eclipse.persistence.jaxb it worked.

Cf. the following snippet for the Maven Bundle Plugin:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <configuration>
                <instructions>
                    <Import-Package>
                        org.eclipse.persistence.jaxb,
                        *
                    </Import-Package>
                </instructions>
            </configuration>
        </plugin>
    </plugins>
</build>

Remote Application Platform (RAP , I have been trying to include the "Apache POI" as dependency and always end up with an at org.eclipse.osgi.internal.serviceregistry. java.lang.​NoClassDefFoundError: Hell..!!! Is this really difficult to add jar in liferay 7.After applying all 4  3 OSGi dependency hell: ClassNotFoundException: org.eclipse.persistence.internal.jaxb.WrappedValue Apr 3 '15 2 How to run different recipes for different sets of dependencies of the same target file Aug 5 '19

You have a missing import package in your OSGi manifest file. Only add this line to your manifest file, this is in the MANIFEST.MF from the bundle where the Role class is located:

DynamicImport-Package: org.eclipse.persistence.internal.*,org.eclipse.persistence.internal.jaxb.*

And BTW, you don't need the annotation @XmlEnum(String.class) above the Role class, leave it as a simple POJO, it will work.

Liferay 7 Dependency Issue with Apache POI, 5 Using Maven to Fetch and Embed Some Transitive Dependencies Transitive Dependencies Into a Mega-Bundle With Eclipse Plug-in Wizard will pull in transitive dependencies automatically, leaving you in classpath hell, by setting the org.osgi.framework.bootdelegation framework property to the  OSGi Access Modifiers. OSGi bundles has its own set of "access modifiers". We've seen those in the bnd.bnd files, at a package level you can choose to export packages or mark them as private. Choices you make along these lines affect what you can extend/override in other bundles.

In addition to @ChristofferSoop answer.

If you work in an eclipse environment, you just need to add the MOXy bundle to your list of dependencies.

Managing OSGi Transitive Dependencies (Part 1), ation of OSGi Dependencies Best Practices in the Eclipse IDE. Request permissions from permissions@acm.org. MSR '18, May 28–29, 2018, the “JAR hell”). OSGi rapidly and application frameworks (e.g., Spring) rely internally on the If this is not the case, a ClassNotFoundException may be thrown when there is a 

[PDF] An Empirical Evaluation of OSGi Dependencies , Why modularity is important, and how Java stacks up; How OSGi enforces some platform for distributed, transactional, interactive, and persistent applications. Every class inside the JAR is externally accessible, no matter how internal its are based on the Eclipse Rich Client Platform, and many application servers 

Chapter 1. OSGi and the enterprise—why now?, Bundles can export services, run processes, and have their dependencies managed. Each bundle has its own internal classpath so that it can serve as an mvn archetype:generate \ -DarchetypeGroupId=org.apache.camel.archetypes Where <PID> is the persistent ID of the OSGi Managed Service (as defined in the 

Deploying into Apache Karaf Red Hat Fuse 7.3, The OSGi Alliance and Standards . Example of an internal JAR dependency. . . . . . . . . . . . . Creating a new Java project in Eclipse: adding the Equinox 1A reference to the term DLL Hell: a comparable problem with managing DLL files on the Bundle A may then choose to import org.foo by declaring.

Comments
  • It doesn't looks like OSGi but more like ClassLoader problem. Can you provide more details about how to reproduce this?
  • @IaroslavSavytskyi getting my app to work in OSGi was a real pain (I admit, a lot of it was caused by Equinox and it's Eclipse tooling). There are 57 bundles in the container just to make a simple Jersey-based REST API + Mongodb. I don't think that it would make sense to try to reproduce it by you or anyone else. If you say it is probably Classloader problem then I'm going to switch back from enum to class and maybe from OSGi to classic web app. Thank you for your comment. I deluded myself that maybe it's some stupid problem with my configuration.
  • It was Equinox, not Felix and Maven was not involved here. The source of problem was probably Equinox's caching mechanism.
  • OK, cool! But please note that even if there was a caching problem involved the error indicates a class loading issue similar to what I experienced (cf the stacktrace). This I did resolve by adding the right package to the OSGI header Import-Package in my bundle's MANIFEST.MF file. If this header was present in your bundle's manifest clearly it was not the source of the error. That I used the maven-bundle-plugin from Felix to do so is incidental to the answer; I might as well have manually edited the manifest before creating the JAR file. Cheers!
  • I can't test it anymore, because MOXy has been dropped from the project, but declaring import of internal packages looks wrong by design.
  • @SewerynNiemiec Remember that the internal package is only for the use of maven-bundle-plugin, it is not an OSGi specification and it tells the plugin only to not export that package and anything else inside it in the manifest, but in this case the MOXy library does export that package (so seems like they didn't use the maven-bundle-plugin after all). And as they have this package exported, it says you can import it. And your compiler is not adding this import automatically because you have no direct dependency to it, so you have to include it manually by adding this line. It works.