Knopflerfish, use external library in bundle

753 Views Asked by At

I know there are two ways of using external libraries in OSGI bundles. As only my package needs the lib (e.g. google-gson) I tried to put it in the Bundle-ClassPath (manifest.mf). But whenever I run the bundle in Knopflerfish I get the Exception in thread "Thread-74" [stderr] java.lang.NoClassDefFoundError: com/google/gson/Gson.

As I am new to OSGI I tried to follow the instructions for creating a bundle from here using a build.xml for compilation where I also included the lib (don't know if this is even correct). So the library appears in three different places, the project classpath, the bundle classpath (manifest.mf) and the build.xml.

I would be glad if someone could give me a hint how to make the library work. Thanks in advance!

2

There are 2 best solutions below

0
On

The process as follows

1.copy all the .jar files and its DEPENDENCIES to a folder

2.mention it in BUNDLE-CLASSPATH in MANIFEST.MF .Refer this

3.Importantly add packages from this library to Export-package element in MANIFEST.MF to make it visible for other bundles.

Hope this resolves your issue

0
On

Few hints that may help and work for me:

  • The MANIFEST.MF of the bundle may have a line like Import-Package: org.osgi.framework. In that line you should add the import of the desired package from the library, so it remains Import-Package: org.osgi.framework, com.google.gson
  • As TheWhiteRabbit said in its answer, libraries should have an export line like Export-Package: its.sec.api.service, so other bundles may see it. Libraries that are already compiled and packaged usually are already setup, but I found some that hasn't so is a good practice to check it.

Also remember that external libraries may be installed into the framework as if it were bundles, i.e. in the init.xargs - install jars/myLib/myLibrary.jar Installed but not started.

Hope this helps