Snippets / HelloWorld aQute - Software Consultancy
Search
*

Hello World (and Goodbye)

Purpose

Demonstrate the simplest possible bundle.

Prerequisites

None

Instructions

The simplest bundle one can make is the "Hello world" example, pioneered by Kernighan and Ritchie. All you need in source code is the following activator:

 Source: aQute/helloworld.HelloWorld.java

 package aQute.helloworld;
 import org.osgi.framework.*;

 public class HelloWorld implements BundleActivator {
   public void start(BundleContext context) throws Exception {
     System.out.println("Hello World");
   }
   public void stop(BundleContext context) throws Exception {
     System.out.println("Goodbye World");
   }
 }

If you run this bundle you will see the "Hello World" and "Goodbye World" on the console, see the note at the end about using the console in OSGi.

This activator must be referenced from the Manifest. The OSGi framework will then call the start() method when the bundle gets started (and stop() when stopped). To build the bundle, you can use the following helloworld.bnd file:

 Bnd file: aQute.helloworld.bnd

 Export-Package: aQute.helloworld
 Bundle-Activator: aQute.helloworld.HelloWorld

Links

  • Using the singleton system console is bad practice in a shared environment; it is only used here for simplicity. Normally the log service should be used, this is shown in the component example.
  • Activation and deactivation is described in the core spec, chapter 4, specifically section 4.3.5.
  • Bundle Activator interface
  • aQute.helloworld.jar
Copyright 2006 aQute SARL, All Rights Reserved