Skip to main content

OSGi Repository for Java Module System

Posted by mandychung on July 24, 2008 at 8:57 AM PDT

The draft specification for supporting OSGi bundles in the Java Module System is currently under discussion in the JSR 277 Expert Group.

To enable the EG and the community to provide feedback, we have included an OSGi repository prototype in the OpenJDK Modules project. The prototype currently supports only Apache Felix OSGi implementation for proof-of-concept. There are open issues to be resolved. The out-of-the-box support is yet to be designed and implemented. Supporting other OSGi implementations is to be investigated. We hope that this prototype enables the community to begin participating and provide feedback for OSGi interoperability support.

We are working on posting a JDK binary built from the modules-dev project to make it ready for you to try out and experiment the JAM modules development as well as the OSGi support. In the meantime, you can clone the source from the Modules mercurial repository and build the JDK.

The OSGi repository for the Java Module System

The OSGi repository is packaged as <JAVA_HOME>/lib/osgi-repo.jar. To build the JDK with the OSGi repository, do the following (refer to the OpenJDK build instructions for details):

   gnumake OSGI_FRAMEWORK_JAR=<pathname of the jar file for the OSGi core API> \
           FELIX_JAR=<pathname of felix.jar> all images 

Note: We will remove the build dependency of the jar files for the OSGi core API and Felix in a future implementation.

Setting up the OSGi repository

The modules-dev implementation provides a repository configuration file (<JAVA_HOME>/lib/module/ to set up the repositories created at VM startup. To set up the OSGi repository:

  1. Download the Apache Felix OSGi implementation.
  2. Uncomment the following lines in the and configure the "osgi.container" property to the pathname of felix.jar and the "osgi.source" property to the desired source location for OSGi bundles:

       osgi.container=file:///<URI of felix.jar>
       osgi.source=file:///<source location where OSGi bundles are loaded from>

You can either update the <JAVA_HOME>/lib/module/ or override the repository properties file by specifying this system property in the command line:<pathname of the file>

Known bug: The VM does not exit when the Felix container has been started unless System.exit() is explicitly. The OSGi repository starts the Felix container during initialization which started a non-daemon thread ("FelixDispatchQueue" thread). Since the FelixDispatchQueue thread is still running, the program will not terminate.

OSGi Repository Initialization

When the OSGi repository is being initialized during the VM initialization, it will first load the configuration properties file that is "conf/" under the parent directory of the directory where felix.jar is located; if it doesn't exist, the default configuration properties will be loaded. Then it will load and start the Felix OSGi framework specified in the "osgi.container" property. All bundles in the source location of the OSGi repository will be automatically installed and started as the OSGi repository initialization.

To start Felix, the bundle cache profile name or directory is required to be specified to indicate where the installed bundles will be cached. If there is no set up, you can set this system property at VM startup:

   -Djava.module.osgi.repository.cache=<bundle cache profile directory name>

Develop a JAM module to use an OSGi bundle

A JAM module that imports an OSGi bundle expresses its dependency in the same way as importing a JAM module. For example, each JAM module has a file to express its module name, version, and its import dependency.

// hello/
   @ImportModule(name="", version="[1.0, 3.0)"),
   @ImportModule(name="", version="2.0+")
module hello;

This "hello" module imports two modules, "" and "" and they are specified in the @ImportModule annotation regardless of whether they are OSGi bundles or JAM modules.

Say "" is an OSGi bundle whereas "" is a JAM module. To successfully resolve the imports, we will need to configure the OSGi repository as an ancestor of the repository for the "hello" module. The source location of the OSGi repository is where the "" and other OSGi bundles are located.

To launch this "hello" module application, it can be as simple as follows:

   <JAVA_HOME>/bin/java< with osgi set up> \
       -Djava.module.osgi.repository.cache=cache \
       -jam hello.jam

You can refer to the documentation in the Modules project for how to create and launch a JAM module in details.

Questions and Feedback

Please send your questions and feedback to

[Comment is closed for this blog because I will not be able to respond to any blog comment the next few weeks (on vacation).]

Related Topics >>