Open main menu

CDOT Wiki β

Your First OSGi Bundle Using CLI

Your First OSGi Bundle Using CLI

This is an example of creating a Hello World OSGi bundle using CLI (Command Line Interface) on Fedora. You can find the original work at [1].

  • Open a terminal window and create this directory path.
    mkdir -p org/foo/example
  • Copy this file, with name Example.java, into the example directory
    package org.foo.example;
    import org.osgi.framework.BundleActivator;
    import org.osgi.framework.BundleContext;
    public class Example implements BundleActivator {
        public void start(BundleContext bc) {
            System.out.println("Hello world");
        }
        public void stop(BundleContext bc) {
            System.out.println("Goodbye world"); 
        }
    }
  • Compile the Java program with this command
    javac -cp /usr/lib64/eclipse/plugins/org.eclipse.osgi_3.6.1.R36x_v20100806.jar org/foo/example/Example.java
  • Copy this file, with name MANIFEST.MF, into your home directory
    Manifest-Version: 1.0
    Bundle-ManifestVersion: 2
    Bundle-SymbolicName: org.foo.example.Example
    Bundle-Version: 1
    Bundle-Activator: org.foo.example.Example
    Import-Package: org.osgi.framework;version="1.3.0"
  • Create a jar file of the manifest and java program with this command
    jar -cvfm example.jar MANIFEST.MF org
  • Now run the bundle using the Equinox console
    java -jar /usr/lib64/eclipse/plugins/org.eclipse.osgi_3.6.1.R36x_v20100806.jar -console
  • You should get the osgi> prompt and your session might look like this
    osgi> ss
    Framework is launched.
    id      State       Bundle
    0       ACTIVE      org.eclipse.osgi_3.6.1.R36x_v20100806
    osgi> install file:example.jar
    Bundle id is 1
    osgi> ss
    Framework is launched.
    id      State       Bundle
    0       ACTIVE      org.eclipse.osgi_3.6.1.R36x_v20100806
    1       INSTALLED   org.foo.example.Example_1.0.0
    osgi> start 1
    Hello world
    osgi> stop 1
    Goodbye world
    osgi> close
  • Congratulations! You've just run your first OSGi bundle using the command line on Fedora Linux.