Arun is a DZone Zone Leader and has posted 65 posts at DZone. You can read more from them at their website. View Full User Profile

How to Create an OSGi Bundle in NetBeans IDE and Deploy to GlassFish

  • submit to reddit

NetBeans has a modular architecture where each module is created as an NBM. OSGi is another popular modular system for Java applications and used by GlassFish to provide a light-weight Java EE 6 compliant application server.

There are a lot of similarities between NBM and OSGi:

  • Runtime container manages lifecycle and dependencies of modules
  • Both are packaged as JAR, metadata stored in META-INF/MANIFEST.MF and some other information
  • Runtime starts up, read meta information and sets up dependencies
  • Every module has its own class loader loading explicitly declared dependencies only

The following presentation explains the similarities and differences between NBM and OSGi very clearly:

Netigso provides a bridge between NBM and OSGi bundles. Using Netigso, NetBeans 6.9 allows you to choose between creating a NBM or an OSGi bundle.

This Tip Of The Day (TOTD) shows how to create an OSGi bundle using NetBeans and deploy in GlassFish.

  1. Download and Install NetBeans 6.9 latest nightly build from here.

  2. Create a new NetBeans module by selecting "New Project..." and picking the values as shown in the image below:

    and click on "Next >".

  3. The module can be created either stand-alone or added to a suite (more on this later). For now, lets create a stand-alone module as shown below:

    and click on "Next >".

  4. On the bundle configuration page, specify the options as shown below:

    and click on "Finish". "Code Name Base" is name of the base package. Notice "Generate OSGi Bundle" is selected which is what will generate the OSGi bundle instead of NBM. The generated directory structure looks like:

    The "" looks like:
    Manifest-Version: 1.0
    Bundle-Localization: hello/Bundle
    Bundle-Name: %OpenIDE-Module-Name
    Bundle-SymbolicName: hello
    Bundle-Version: 1.0

  5. Right-click on "Source Packages", select "New", "Installer / Activator ..." as shown below:

    Take the default values as shown below:

    and click on "Finish". The generated source code looks like:

    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    package hello;

    import org.osgi.framework.BundleActivator;
    import org.osgi.framework.BundleContext;

    * Manages a bundle's lifecycle. Remember that an activator is optional and
    * often not needed at all.
    public class Installer implements BundleActivator {

    public void start(BundleContext c) throws Exception {

    public void stop(BundleContext c) throws Exception {


    The code that needs to be executed during starting and stopping this OSGi bundle can now be added to "start" and "stop" methods respectively. To keep it simple, add the following line to "start" method:
    System.out.println("OSGi Bundle from NetBeans: Started");

    and the following to "stop" method:
    System.out.println("OSGi Bundle from NetBeans: Stopped");
  6. Right-click on the project and select "Create NBM" as shown below:

    This creates "hello.jar" in the "build" directory of project.

  7. Fire up your GlassFish as "asadmin start-domain" and copy "hello.jar" to "glassfishv3/glassfish/domains/domain1/autodeploy/bundles" directory to deploy the bundle. It shows a log statement as:
    /autodeploy/bundles/, felix.fileinstall.filename=org.apache.felix.fileinstall-autodeploy-bundles.cfg,
    service.factorypid=org.apache.felix.fileinstall, felix.fileinstall.debug=1};|OSGi Bundle from
    NetBeans: Started
    The message printed from our generated bundle is highlighted in the bold. TOTD #118 shows other ways to manage OSGi bundles in GlassFish.

If you'd like to create multiple OSGi bundles that are related to each other then you create a "Module Suite" as shown below:

Multiple modules can be added by right-clicking on "Modules"

and either creating a new module or adding an existing one. Make sure to check "Generate OSGi Bundle" for the newly created bundle as well. Once the modules are added, then you can right-click on the suite, select "OSGi" and build all the modules in the suite together by selecting "Build Bundles" as shown below:

All the modules are now created in "/build/cluster/modules" directory of your suite.

Clicking on "Build Bundle Repository" creates an OSGi Bundle Repository (a federated repository of bundles).

Technorati: totd netbeans glassfish osgi nbm module

Published at DZone with permission of its author, Arun Gupta.