Non-osgi library usage in an osgi application

Yes, it is possible. You have two options:

Firstly, you can include all packages from external library into private-package section of your bundle. It will include all this packages into jar with your application. The second option is to make a valid osgi bundle from external library.


Yes, you can either embed the external library in your bundle or wrap ("OSGIfy") the library as an OSGi bundle. For both options, Pax Construct (http://www.ops4j.org/projects/pax/construct) is a good tool.

If your external library itself has dependencies, embed all of these in a single bundle or use Pax Construct to wrap them transitive.

If have to to choose between wrapping or embedding, consider dependency management and versioning of bundles. If you need to upgrade the external library and it's embedded in your own application bundle, you always release both the library and your own code. It is e.g also not possible to have 2 version of the library active without having 2 version of your application bundle active. And also... if you are not working in an OSGi environment, would you like to add third-party classes in your application jars? So why do it in an OSGi environment?

I personally prefer in such a case to see the external lib as a black box, and wrap the library and it's dependencies in a single bundle.


As it was written in previous answers you have two options if you want to use additional libraries in your bundles:

  1. embedding library jars in a bundle in which it will be used,
  2. creating a valid OSGi bundle from the library.

The first approach is simpler because you only need to copy library jars (and all its dependencies) to a bundle (e.g. to a root directory) and then add them to Bundle-Classpath element in MANIFEST.MF (see here). However, while doing this you must remember that this added library will be visible only in a bundle in which it is embedded (so library reuse is limited). You could always add packages from this library to Export-package element in MANIFEST.MF to make it visible for other bundles but this is far from elegant solution (however it will work).

In order to make it visible to other bundles you should use the second approach, i.e. create an OSGi bundle from the library (there are tools which can help you in doing that, also in Eclipse). However, for more complicated libraries this approach may be harder (because of dependencies and specific class loading approach in OSGi).

So if you want to use the library only in one bundle I suggest using the first approach (it is easier to implement). If you want to use this library in many bundles in your application you should consider the second approach.