Learning Apache Karaf

2 (2 reviews total)
By Jamie Goodyear , Johan Edstrom , Heath Kesler
  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies

About this book

Apache Karaf is more than just an OSGi-based runtime container; it’s an ecosystem of open source technologies that makes operating and managing applications easier. Karaf supports hot deployment of OSGi bundles by monitoring jar files inside the [home]/deploy directory. Each time a jar is copied in this folder, it will be installed inside the runtime. You can then update or delete it, and changes will be handled automatically. In addition, Karaf also supports exploded bundles and custom deployers.

Learning Apache Karaf will introduce developers and administrators alike to the OSGi-based container, and impart best practices by example. The book shows you how to set up an Apache Karaf distribution, and how to arrive at a production-ready deployment. Learning Apache Karaf teaches you everything you need to install, configure, and administer Apache Karaf.

Learning Apache Karaf will help guide you through the complexity of an OSGi container using sample code and configuration snippets to demonstrate the important aspects of managing and running a Karaf instance.

Learning to install and command the runtime is just the beginning; deploying and provisioning applications is just as important to the successful implementation of a stable and scalable architecture. You will discover how to monitor runtime environments using remote access, logging, and JMX, and you will also develop production-level architectures with high availability and security.

Publication date:
October 2013


Chapter 1. Installing Apache Karaf

Before Apache Karaf can provide you with an OSGi-based container runtime, we'll have to set up our environment first. The process is quick, requiring a minimum of normal Java usage integration work.

In this chapter we'll review:

  • The prerequisites for Apache Karaf

  • Obtaining Apache Karaf

  • Installing Apache Karaf and running it for the first time



As a lightweight container, Apache Karaf has sparse system requirements. You will need to check that you have all of the below specifications met or exceeded:

  • Operating System: Apache Karaf requires recent versions of Windows, AIX, Solaris, HP-UX, and various Linux distributions (RedHat, Suse, Ubuntu, and so on).

  • Disk space: It requires at least 20 MB free disk space. You will require more free space as additional resources are provisioned into the container. As a rule of thumb, you should plan to allocate 100 to 1000 MB of disk space for logging, bundle cache, and repository.

  • Memory: At least 128 MB memory is required; however, more than 2 GB is recommended.

  • Java Runtime Environment (JRE): The runtime environments such as JRE 1.6 or JRE 1.7 are required. The location of the JRE should be made available via environment setting JAVA_HOME. At the time of writing, Java 1.6 is "end of life".


For our demos we'll use Apache Maven 3.0.x and Java SDK 1.7.x; these tools should be obtained for future use. However, they will not be necessary to operate the base Karaf installation. Before attempting to build demos, please set the MAVEN_HOME environment variable to point towards your Apache Maven distribution.

After verifying you have the above prerequisite hardware, operating system, JVM, and other software packages, you will have to set up your environment variables for JAVA_HOME and MAVEN_HOME. Both of these will be added to the system PATH.


Setting up JAVA_HOME Environment Variable

Apache Karaf honors the setting of JAVA_HOME in the system environment; if this is not set, it will pick up and use Java from PATH.

For users unfamiliar with setting environment variables, the following batch setup script will set up your windows environment:

@echo off
REM execute setup.bat to setup environment variables.
set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_31
set MAVEN_HOME=c:\x1\apache-maven-3.0.4
set PATH=%JAVA_HOME%\bin;%MAVEN_HOME%\bin;%PATH%echo %PATH%

The script creates and sets the JAVA_HOME and MAVEN_HOME variables to point to their local installation directories, and then adds their values to the system PATH. The initial echo off directive reduces console output as the script executes; the final echo command prints the value of PATH.


Managing Windows System Environment Variables

Windows environment settings can be managed via the Systems Properties control panel. Access to these controls varies according to the Windows release.

Conversely, in a Unix-like environment, a script similar to the following one will set up your environment:

# execute setup.sh to setup environment variables.
echo $PATH

The first two directives create and set the JAVA_HOME and MAVEN_HOME environment variables, respectively. These values are added to the PATH setting, and then made available to the environment via the export command.


Obtaining Apache Karaf distribution

As an Apache open source project, Apache Karaf is made available in both binary and source distributions. The binary distribution comes in a Linux-friendly, GNU-compressed archive and in Windows ZIP format. Your selection of distribution kit will affect which set of scripts are available in Karaf's bin folder. So, if you're using Windows, select the ZIP file; on Unix-like systems choose the tar.gz file.

Apache Karaf distributions may be obtained from http://karaf.apache.org/index/community/download.html. The following screenshot shows this link:

The primary download site for Apache Karaf provides a list of available mirror sites; it is advisable that you select a server nearer to your location for faster downloads.

For the purposes of this book, we will be focusing on Apache Karaf 2.3.x with notes upon the 3.0.x release series.


Apache Karaf 2.3.x versus 3.0.x series

The major difference between Apache Karaf 2.3 and 3.0 lines is the core OSGi specification supported. Karaf 2.3 utilizes OSGi rev4.3, while Karaf 3.0 uses rev5.0. Karaf 3 also introduces several command name changes. There are a multitude of other internal differences between the code bases, and wherever appropriate, we'll highlight those changes that impact users throughout this text.


Installing Apache Karaf

The installation of Apache Karaf only requires you to extract the tar.gz or .zip file in your desired target folder destination.

The following command is used in Windows:

unzip apache-karaf-<version>.zip

The following command is used in Unix:

tar –zxf apache-karaf-<version>.tar.gz

After extraction, the following folder structure will be present:

  • The LICENSE, NOTICE, README, and RELEASE-NOTES files are plain text artifacts contained in each Karaf distribution. The RELEASE-NOTES files are of particular interest, as upon each major and minor release of Karaf, this file is updated with a list of changes.

  • The bin folder contains the Karaf scripts for the interactive shell (Karaf), starting and stopping background Karaf service, a client for connecting to running Karaf instances, and additional utilities. These scripts will be described in later chapters.

  • The data folder is home to Karaf's logfiles, bundle cache, and various other persistent data.

  • The demos folder contains an assortment of sample projects for Karaf. It is advisable that new users explore these examples to gain familiarity with the system. For the purposes of this book we strived to create new sample projects to augment those existing in the distribution.

  • The instances folder will be created when you use Karaf child instances. It stores the child instance folders and files.

  • The deploy folder is monitored for hot deployment of artifacts into the running container. In Chapter 5, Deploying Applications, we will explore this feature in depth.

  • The etc folder contains the base configuration files of Karaf; it is also monitored for dynamic configuration updates to the configuration admin service in the running container. Karaf configuration details will be explored in Chapter 3, System Configuration and Tuning.

  • An HTML and PDF format copy of the Karaf manual is included in each kit.

  • The lib folder contains the core libraries required for Karaf to boot upon a JVM.

  • The system folder contains a simple repository of dependencies Karaf requires for operating at runtime. This repository has each library jar saved under a Maven-style directory structure, consisting of the library Maven group ID, artifact ID, version, artifact ID-version, any classifier, and extension.


First boot!

After extracting the Apache Karaf distribution kit and setting our environment variables, we are now ready to start up the container. The container can be started by invoking the Karaf script provided in the bin directory:

On Windows, use the following command:


On Unix, use the following command:


The following image shows the first boot screen:

Congratulations, you have successfully booted Apache Karaf! To stop the container, issue the following command in the console:

[email protected]> shutdown –f

The inclusion of the –f or –-force flag to the shutdown command instructs Karaf to skip asking for confirmation of container shutdown.


Pressing Ctrl + D will shut down Karaf when you are on the shell; however, if you are connected remotely (using SSH), this action will just log off the SSH session, it won't shut down Karaf.



We have discovered the prerequisites for installing Karaf, which distribution to obtain, how to install the container, and finally how to start it running.

In the next chapter we will introduce the Karaf command line, review some of the most commonly used commands, and ways to access them.

About the Authors

  • Jamie Goodyear

    Jamie Goodyear is an open source advocate, Apache developer, and computer systems analyst with Savoir Technologies. He has designed, critiqued, and supported architectures for large organizations worldwide.

    Jamie holds a bachelor of science degree in computer science from Memorial University of Newfoundland.

    Jamie has worked in systems administration, software quality assurance, and senior software developer roles for businesses ranging from small start-ups to international corporations. He has attained committer status on Apache Karaf, Servicemix, and Felix and is a project management committee member on Apache Karaf. His first print publication was co-authoring Packt Publishing's Instant OSGi Starter, followed by co-authoring Packt Publishing's Learning Apache Karaf, and Packt Publishing's Apache Karaf Cookbook.

    Currently, he divides his time between providing high-level reviews of architectures, mentoring developers and administrators with SOA deployments, and helping grow the OpenDaylight and Apache communities.

    Browse publications by this author
  • Johan Edstrom

    Johan Edstrom is an open source software evangelist, Apache developer, and seasoned architect; he has created Java architectures for large, scalable, high transaction monitoring, financial, and open source systems. He is a trained electronics engineer with a penchant for fractal geometry.

    He has worked as a development lead, infrastructure manager, IT lead, and programmer, and has guided several large companies to success in the use of open source software components. Lately, he has been helping some of the world's largest networking companies and medical start-ups achieve high availability, scalability, and dynamically adapting SOA systems.

    Johan divides his time between writing software, mentoring development teams, and teaching people how to use Apache ServiceMix, Camel, CXF, and ActiveMQ effectively and in a scalable way for enterprise installations.

    Johan is the co-author along with Jamie Goodyear of Instant OSGi Starter, Packt Publishing.

    Johan is a committer on Apache Camel and Apache ServiceMix and is a Project Management Committee member for Apache Camel.

    Browse publications by this author
  • Heath Kesler

    Heath Kesler is an Apache developer and committer, has spoken at conferences around the world, and is a senior SOA architect with Savoir Technologies. He has architected and developed scalable, highly available SOA systems for large corporations around the globe.

    He currently helps corporations implement and develop enterprise integration systems using messaging and web services with a focus on maintainability and scalability. He gives training classes on complex concepts and frameworks that provide functionality to large-scale enterprise solutions. He has bootstrapped development on mission critical systems for several Fortune 500 companies.

    He has reached committer status on Apache Karaf and is a contributor to Camel. He received a Bachelor of Science degree from DeVry University after his tour in the army.

    Browse publications by this author

Latest Reviews

(2 reviews total)
Livro muito ruim..........
Not finding the examples I need in this.
Book Title
Access this book, plus 7,500 other titles for FREE
Access now