How to embed third party jars in OSGI bundle

If you are writing code which is using some third party jars, you would like to embed them in the final bundle.

So here is a sample pom.xml which helps in embedding. My code was using a third party library : restfb. In dependency I have specified that.

Now to include it in the final bundle we need to make use of <embed-dependency>. Here I have specified that as *;scope=compile. So it will embed all the dependencies whose scope was specified as compile which is a default scope.

restfb’s scope is compile so it will be embedded in the bundle. org.osgi.core’s scope is provided, so it will not be embedded. We should not even embed osgi jars as it will be provided by the platform itself. If we embed them, you might start getting classcastexception as Activator class of OSGI might get loaded from platform as well as your embedded jar.

In above example we had embedded osgi core jar also. So Activator class will extend the BundleActivator class of this jar.

Now if use felix container to start this bundle, it will also have osgi core jar. It will expect Activator class to extend the BundleActivator class of this jar but in actual it will be of type BundleActivator present in osgi core jar inside the bundle. Hence you will get classcastexception.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""

	<!-- Licensed to the Apache Software Foundation (ASF) under one or more 
		contributor license agreements. See the NOTICE file distributed with this 
		work for additional information regarding copyright ownership. The ASF licenses 
		this file to you under the Apache License, Version 2.0 (the "License"); you 
		may not use this file except in compliance with the License. You may obtain 
		a copy of the License at Unless 
		required by applicable law or agreed to in writing, software distributed 
		under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES 
		OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
		the specific language governing permissions and limitations under the License. -->



	<name>simpleBundleTest Bundle</name>
        simpleBundleTest OSGi bundle project.



		<!-- -->




Uday Ogra

Connect with me at and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *