Write simple Karaf OSGI bundles which interact with each other in eclipse in 10 minutes

STEP 1 – Download and extract Karaf

Download the last Stable Version from https://karaf.apache.org/download.html
Extract the Zip to location of your Choice.

STEP 2 – Create a new project in Eclipse

  1. Click on New Project and select Maven Project
    Lets configure a new archtype for Maven.We need to do this only once.
    Select add Archetype and fill the Dialog as follows :

By creating this archeType being provided by Karaf guys, the pom.xml file of the project will be pre-filled with OSGI related stuffs like dependencies and all. We can skip this step but then we will have to manually fill the pom.xml file with OSGI related dependencies and plugins.

So now we can can choose the Karaf Bundle Archtype and click Next.

Give any group ID. This will be initials of your package.

Step 3: Write some Code

Generated project will have an Activator class. This class acts as an entry point to this bundle. Its start and stop methods will be invoked whenever this bundle would be started and stopped respectively.

We have PallindromeService interface which will be implemented by this class. The implementing object will be registered as a service which can be invoked by other bundles

package org.ogra.bundle1;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

public class Activator implements BundleActivator, PallindromeService {
	private ServiceRegistration serviceReg;

	public void start(BundleContext context) {
		System.out.println("Starting Bundle1 ......");
		serviceReg = context.registerService(PallindromeService.class.getName(), this, null);

	public void stop(BundleContext context) {
		System.out.println("Stopping Bundle1 ");

	public boolean isPallindrome(String a) {
		int n = a.length();
		String b = "";
		for (int i = n - 1; i >= 0; i--) {
			b = b + a.charAt(i);

		return b.equalsIgnoreCase(a);


Here is the Pallindrome interface

package org.ogra.bundle1;

public interface PallindromeService {

	public boolean isPallindrome(String str) ;

Here is how pom.xml will look like. bundle-activator will be the activator class which will act as an entry point.We are also exporting the package org.ogra.bundle1 so that other bundles can import it.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">



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






You can do run as –> maven install. It will create the OSGI bundle of the project and add to your local maven repo.

Our Bundle is now prepared to run inside Karaf.


Start Karaf in Debugging Mode by typing ./karaf debug in your KARAF/bin Folder.

Now install bundle you had created in the previous step.

It will return unique bundle ID of this bundle.

Step 6 : Create new bundle

Repeat all the steps of bundle creation and create bundle2. The activator class of bundle2 will consume the service exposed by the bundle1

package org.ogra.bundle2;

import org.ogra.bundle1.PallindromeService;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

public class Activator implements BundleActivator {
	private ServiceReference serviceReg;

	public void start(BundleContext context) {
		System.out.println("Starting Bundle2 ");
		serviceReg = context.getServiceReference(PallindromeService.class.getName());
		PallindromeService ms = (PallindromeService) context.getService(serviceReg);
		boolean b = ms.isPallindrome("abccba");


	public void stop(BundleContext context) {
		System.out.println("Stopping Bundle2 ");


Also install it to Karaf the way we did for bundle1.

Step 7 : Start the bundles

We can use start command in the karaf console. Lets start bundle1 whose ID after installation was 77. It will print some debug info which we had written in start method of its activator class

Now start bundle2. It will invoke PallindromeService present in bundle1 and print if the text is pallindrome or not

That brings us to the end of the tutorial. This is how simple it is to build OSGI bundles, deploy them on Karaf and make them interact with each other

Uday Ogra

Connect with me at http://facebook.com/tendulkarogra and lets have some healthy discussion :)

You may also like...

Leave a Reply

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