How to integrate an external library?

Hi,
I want to use an external jar library in a newly created bundle. Where do I have to set the according build path for the external library and what else do I need to do?

Greets,
Nils

Hi Nils,

in an ideal case, this external library is available via maven and built for OSGi. In that case add the dependency to the pom.xml (https://github.com/OpenEMS/openems/blob/develop/cnf/pom.xml). Bndtools will take care of downloading the jar-file. Then open bnd.bnd file, the tab Build and add the jar as an entry to the Build Path using the + symbol.

You can recognize if your jar-dependency is ready for OSGi checking if the META-INF/MANIFEST.MF file inside the jar-file. It should have an Export-Package entry.

If your library is not directly available as an OSGi jar, it might be already OSGi-ified by the apache servicemix project (https://servicemix.apache.org/).

If it is really not available as an OSGi jar, you need to write a wrapper. See https://github.com/OpenEMS/openems/tree/develop/io.openems.wrapper for examples.

If your jar-file is not available on maven, copy the jar-file to your bundle project. Then add

-includeresource: \
	@lib/filename.jar; lib:=true

-buildpath: \
    ${buildpath},\
    lib/filename.jar;version=file

to your bnd.bnd file.

The the bndtools docs for more information:

Hope it works for you.
Regards,
Stefan

Hi Stefan,
thank you for the response! The library I want to use is the JSON-simple library. I pasted the following entry into the pom.xml:


com.googlecode.json-simple
json-simple
1.1

I also added it into the bnd.bnd build path of the new bundle. Unfortunately it still says: Unresolved requirement: Import-Package: org.json.simple

The MANIFEST.MF of that bundle only has the entry to the corresponding api I created. Should there be another entry?
Thank you for your help!

Solved it:
There need to be an entry made to the EdgeApp.bndrun. Under source I added in -runbundles the following entry:
com.googlecode.json-simple;version=’[1.1.1,2.0.0)’,
Now it seems to work.

Great it worked for you. Just two notes:

  • Clicking the “Resolve” button in “EdgeApp.bndrun” should have generated that entry in “-runbundles” automatically.
  • OpenEMS is using JSON format in many places (e.g. for the JSON-RPC-communication between Backend, Edge and UI). I made good experiences and can recommend the GSON library by google. There is also a powerful JsonUtils class that provides a lot of helper functions.