Wednesday, 30 July 2014

How to Convert a jar File into an OSGi Bundle

Step 1:


Start by creating a the jar's manifest file:
Manifest-Version: 1.0
Created-By: myself
Bundle-ManifestVersion: 2
Bundle-Name: JUnit 4.4 bundle
Bundle-Description: Package junit 4.4 in an OSGi bundle
Bundle-Version: 4.4.0
Bundle-ClassPath: .,junit-4.4.jar
Bundle-SymbolicName: org.junit.framework
Export-Package: junit.framework,junit.extensions,org.junit.runner,org.junit,junit.textui


Step 2 :
Create the bundle jar file by running the following command:
jar cvfm junit-4.4-bundle.jar manifest.txt junit-4.4.jar 

Where manifest.txt is the name of the manifest file created above.


2 comments:

  1. Can you please elaborate a bit more on this post? Which jar are we trying to convert to a bundle? Why is the extension "txt"? What are the compulsory attributes? Where is this command supposed to be executed? How is the bundle generated? Is it some maven plugin that does it similar to creating a new bundle using maven archetype?

    ReplyDelete
    Replies
    1. Hi Manish,

      Ex: you have normal jar, And it doesn't have any meta data info. So, this jar won't install in OSGI container because OSGI container requires meta data.

      But as per example requirement, you need this jar to be install OSGI container, because you are importing some of these classes into your project.

      Here is the solution to resolve dependency:

      Note: we are taking example of junit jar.

      Step 1: Navigate to your bundle download location, And create manifest.txt file in your local, and add below content to .txt file

      Manifest-Version: 1.0
      Created-By: myself
      Bundle-ManifestVersion: 2
      Bundle-Name: JUnit 4.4 bundle
      Bundle-Description: Package junit 4.4 in an OSGi bundle
      Bundle-Version: 4.4.0
      Bundle-ClassPath: .,junit-4.4.jar
      Bundle-SymbolicName: org.junit.framework
      Export-Package: junit.framework,junit.extensions,org.junit.runner,org.junit,junit.textui

      Step 2: open command promt and navigate to your bundle location, excute below command

      jar cvfm junit-4.4-bundle.jar manifest.txt junit-4.4.jar


      cvfm means "create a jar; show verbose output; specify the output jar file name; specify the manifest file name."
      junit-4.4-bundle.jar = new OSGI jar
      manifest.txt = you have created this file
      junit-4.4.jar = normal jar file which you want to convert as OSGI

      Delete