Hello, OSGi, Part 1: Bundles for beginners

Creating, executing, and managing bundles in an OSGi container

1 2 3 4 5 Page 3
Page 3 of 5

The OSGi console

The OSGi console is a command-line interface to the OSGi container. It allows you to do things like start, stop, install bundles, and update or delete bundles. In your Eclipse IDE, click the console view to give focus to that view, then click Enter and you will get an OSGi prompt like the one shown in Figure 2 (click to enlarge).

A screenshot of the OSGi console.
Figure 2. HelloWorldActivator.java in the OSGi console

Here are some of the commonly used OSGi commands that you can use to interact with your OSGi container:

  • ss displays a list of installed bundles with the status of each bundle. It will display the bundle ID, short name, and status of the bundle.
  • start <bundleid> starts a bundle.
  • stop <bundleid> stops a bundle.
  • update <bundleid> updates a bundle with a new JAR file.
  • install <bundleURL> installs a new bundle into the OSGi container.
  • uninstall <bundleid> uninstalls already installed bundles from the OSGi container.

Note that these commands are defined in the OSGi specification so you can use them to interact with any of the OSGi containers.

Dependency management

The OSGi specification allows you to break your application into multiple modules and then manage their dependencies on each other. It does this by adding a bundle scope. By default, none of the classes in a bundle are visible from any other bundle; inside bundles they follow the normal rules of the Java language. So, what do you do if you want to access the classes of one bundle from another bundle? The solution is to export packages from the source bundle and then import them into the target bundle. In this section we'll walk through a sample application that demonstrates this concept.

First, we will create a com.javaworld.sample.HelloService bundle, which will export a package. Then we will import the package into our com.javaworld.sample.HelloWorld bundle.

Exporting a package

We'll start by creating the com.javaworld.sample.HelloService bundle and exporting a package from it. Follow these steps to create the bundle and export the package:

  1. Create the com.javaworld.sample.HelloService bundle by following the same steps used to create the com.javaworld.sample.HelloWorld bundle in the previous section.
  2. Inside the HelloService bundle, create a com.javaworld.sample.service.HelloService.java interface, as shown in Listing 3.

    Listing 3. HelloService.java

    public interface HelloService {
        public String sayHello();
  3. Now create a com.javaworld.sample.service.impl.HelloServiceImpl.java class implementing the HelloService interface, as shown in Listing 4.

    Listing 4. HelloServiceImpl.java

    public class HelloServiceImpl implements HelloService{
        public String sayHello() {
            System.out.println("Inside HelloServiceImple.sayHello()");
            return "Say Hello";
  4. Open the MANIFEST.MF for the HelloService package in your Eclipse Manifest editor and go to the Runtime tab. In the Exported Packages section, click Add and select the com.javaworld.sample.service package. The MANIFEST.MF file for the HelloService bundle should look like the one in Listing 5.

    Listing 5. Manifest for HelloService bundle

    Manifest-Version: 1.0
     Bundle-ManifestVersion: 2
     Bundle-Name: HelloService Plug-in
     Bundle-SymbolicName: com.javaworld.sample.HelloService
     Bundle-Version: 1.0.0
     Bundle-Vendor: JAVAWORLD
     Bundle-Localization: plugin
     Export-Package: com.javaworld.sample.service
     Import-Package: org.osgi.framework;version="1.3.0"

As you can see, the MANIFEST.MF file for the HelloService bundle looks very similar to that of the HelloWorld bundle. The only difference is that this MANIFEST.MF file has one Export-Package manifest header, whose value is com.javaworld.simple.service.

The Export-Package manifest header informs the OSGi container that classes in the com.javaworld.sample.service package from the HelloService bundle can be accessed from outside. Note that in the sample code we have exposed a HelloService interface but not the HelloServiceImpl implementation class.

Importing a package

The next step is to update the HelloWorld bundle to import the com.javaworld.simple.service package. Here are the steps to import the package:

  1. In the


    bundle, open the


    file in the Plug-in Manifest editor. Now go to the Dependencies tab, and then to

    Imported Packages

    . Add


    as the value of Imported Packages. The


    file for your


    bundle should look like the one in Listing 6.

    Listing 6. Manifest for the updated HelloWorld bundle

    Manifest-Version: 1.0
    Bundle-ManifestVersion: 2
    Bundle-Name: HelloWorld Plug-in
    Bundle-SymbolicName: com.javaworld.sample.HelloWorld
    Bundle-Version: 1.0.0
    Bundle-Activator: com.javaworld.sample.helloworld.Activator
    Bundle-Vendor: JAVAWORLD
    Bundle-Localization: plugin
    Import-Package: com.javaworld.sample.service,

    As you can see, the value of the Import-Package header is a comma-separated list of packages that this bundle wants to import. In the sample code the HelloWorld bundle imports two packages: com.javaworld.sample.service and org.osgi.framework.



    package contains OSGi framework classes such as




    , which are used by the


    class of the



  2. Next, open com.javaworld.sample.helloworld.Activator.java in the Eclipse Java editor. You will notice that you are now able to access the HelloService interface but not the HelloServiceImpl class. This is because the HelloService package exports (and the HelloWorld package imports) the com.javaworld.sample.service package. HelloServiceImpl is an internal class for the HelloService bundle and no other bundle can access it.
1 2 3 4 5 Page 3
Page 3 of 5
How to choose a low-code development platform