Deepak Nadig Anantha

Recent Posts

Recent Comments




OSGi and Apache Karaf: A how-it’s-done Tutorial

Deepak NadigDeepak Nadig



Java 8 and Maven

We’ll start by installing Oracle Java 8 JDK and Maven. Although, the following instructions are for Ubuntu, I’ll include helpful links at the end of the post for setup instructions on other platforms like Windows or Mac OS X, so let’s begin:

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer oracle-java8-set-default

You should now have Oracle Java 8 JDK installed. Let’s quickly check the Java version by using:

java -version
java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)

Now install Maven by issuing the command:

sudo apt-get install maven

Install Docker

Let’s just use the script at to install the Docker Engine.

curl -sSL | sudo sh

We can check if the Docker Engine is running by using the command:

sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
535020c3e8ad: Pull complete
af340544ed62: Pull complete
Digest: sha256:a68868bfe696c00866942e8f5ca39e3e31b79c1e50feaee4ce5e28df2f051d5c
Status: Downloaded newer image for hello-world:latest

Hello from Docker.
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
1. The Docker Engine CLI client contacted the Docker Engine daemon.
2. The Docker Engine daemon pulled the "hello-world" image from the Docker Hub.
3. The Docker Engine daemon created a new container from that image which runs the
   executable that produces the output you are currently reading.
4. The Docker Engine daemon streamed that output to the Docker Engine CLI client, which sent it
   to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker Hub account:

For more examples and ideas, visit:

Working with Apache Karaf

There is one more thing we have to do before we can start building OSGi packages. We will use Apache Karaf to run a standalone container to host our OGSi application. Karaf features a shell console, remote access, hot deployment, dynamic configuration and provides additional capabilities like clustering, complete monitoring and alerting, application repository etc. For our purposes, we’ll use an Apache Karaf Docker container from Docker Hub. To start the Karaf container, fire up a terminal and type:

docker run -it -p 8101:8101 -p 8181:8181 -p 44444:44444 -p 1099:1099 mkroli/karaf

The -p option is used to expose and forward ports from the Docker container to the host machine. You should see the following prompt:

Unable to find image 'mkroli/karaf:latest' locally
latest: Pulling from mkroli/karaf

8ad8b3f87b37: Pull complete 
751fe39c4d34: Pull complete 
ae3b77eefc06: Pull complete 
8b357fc28db9: Pull complete 
1a614fcb4b1b: Pull complete 
1fcd29499236: Pull complete 
1df99ed2f401: Pull complete 
c4b6cf75aef4: Pull complete 
3cdfe48fa24f: Pull complete 
Digest: sha256:ef01b93a0ba005f9b92bcf762567b0fed72c21f5242b93a61a3e4e610b1a70a0
Status: Downloaded newer image for mkroli/karaf:latest
        __ __                  ____      
       / //_/____ __________ _/ __/      
      / ,<  / __ `/ ___/ __ `/ /_        
     / /| |/ /_/ / /  / /_/ / __/        
    /_/ |_|\__,_/_/   \__,_/_/         

  Apache Karaf (4.0.5)

Hit '<tab>' for a list of available commands
and '[cmd] --help' for help on a specific command.
Hit '<ctrl-d>' or type 'system:shutdown' or 'logout' to shutdown Karaf.


Now that Karaf is up and running, you can install webconsole to easily manage bundles and services. to install webconsole, you can type the following at the Karaf prompt:

karaf@root()> feature:install webconsole

webconsole should now be accessible from your browser at http://localhost:8181/system/console with the username/password of karaf/karaf.

Generating Bundles using Maven

Maven is a software project management and comprehension tool based on the concept of a project object model (POM). Maven can help you manage your project’s build, reporting and documentation centrally. Let us use Maven Archetypes to generate skeleton code for our bundle. To do this, at the terminal input:

mvn archetype:generate

For example, let’s choose the karaf-bundle-archetype

816: remote -> org.apache.karaf.archetypes:karaf-bundle-archetype (A simple bundle archetype.)

You can now choose the latest version number, enter groupId (same as package name), artifactId (similar to a class name), and choose the default settings for the rest:

Define value for property 'groupId': :
Define value for property 'artifactId': : myapp
Define value for property 'version':  1.0-SNAPSHOT: : 
Define value for property 'package': : 
[INFO] Using property: package =
Confirm properties configuration:
artifactId: myapp
version: 1.0-SNAPSHOT
 Y: : 
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: karaf-bundle-archetype:4.0.6
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value:
[INFO] Parameter: artifactId, Value: myapp
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value:
[INFO] Parameter: packageInPathFormat, Value: org/foo
[INFO] Parameter: package, Value:
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: groupId, Value:
[INFO] Parameter: artifactId, Value: myapp
[INFO] project created from Archetype in dir: /home/ubuntu/myapp
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 05:19 min
[INFO] Finished at: 2016-09-22T23:09:59+00:00
[INFO] Final Memory: 15M/139M
[INFO] ------------------------------------------------------------------------

We can now build the bundle using:

cd myapp/
mvn clean install

This will build the bundle and create a jar file inside the target/ folder. You can now install this bundle using the Karaf webconsole.
You’ve now installed and started an OSGi bundle. Happy coding!

Why docker? and karaf webconsole?

Although I used a docker container to access Karaf, this step was used just to introduce docker usage. You can absolutely run a Karaf container without docker and HERE are some instructions to get you started. Also, instead of using the Karaf webconsole, you can directly install the bundle using the Karaf prompt as:

karaf@root()> bundle:install -s mvn:<path-to-bundle>/target/<bundle-name>.jar

Helpful Links

Comments 0
There are currently no comments.