Search code examples
javanoclassdeffounderrorosgi-bundlemaven-bundle-plugin

OSGi Import Package issues


I'm developing a bundle with some third party dependencies. The bundle refers the core and connection functionalities from two different jars of that third-party.

Then i do the Import-Package with maven-bundle-plugin i could only consume a single import package from any of the jar and not both at the same time (referred other threads and couldn't get a clear understanding of the JAR import). Or is it a problem with the package organization of the third-party?

JAR A -> package -> com.test.pkg [contains class1, class2] JAR B -> package -> com.test.pkg [contains class3]

The Import-package imports com.test.pkg, but my application is in need for class3, and when during run-time my containers throws a ClassDefNotFoundException when class3 was referred. Not sure i've understood the concept wrong.

POM snippet is as below

<Import-Package>*,com.test.pkg</Import-Package>

Is there a way I can tell to my OSGi container to get the package from specific JAR.?


Solution

  • This is called 'split package' and is why it is a bad practice in OSGi to duplicate a package name in multiple bundles.

    There are a couple of ways to resolve your issue. The easiest is to package both of the 3rd party jars into a new jar, and then export what you need from the uber-jar. You can unpack the jars into source folders, or include the original jars in a lib folder. Then include a manifest that exports what you need.

    If you only need to bind your OSGi client to only one of the jars, you can use require-bundle instead of import-package. This is not the best design, as it forces you to specify a bundle instead of a package, but sometimes it is the best option.

    Finally, you can use the split-package directive to give more control over the binding process. If you don't want to repackage the 3rd party jars with an updated manifest, you will have to create a fragment bundle to specify the split package directives there.