Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding JARs in an OSGi Bundle in Eclipse

I need to embed a third-party JAR file in the OSGi bundle that I'm developing in Eclipse. I've done a lot of searching and reading, and I keep seeing the same things over and over again:

  1. I really shouldn't be doing this. It isn't the OSGi "way" of doing things.
  2. It's actually pretty easy to do. Just embed the JAR at the root of your bundle and specify Bundle-ClassPath: ., jar_filename.jar in your manifest.

Okay. Except the JAR I'm embedding is tiny and unlikely to be used by anything else I will ever implement on this closed system, so it's a lot easier to embed it so that users only have to download one bundle (ease of use for the end-user is vital). So I'm going to go against convention and embed it.

Now, in the IDE, everything is kosher. I dropped the JAR in the src folder of my Eclipse project and configured the project's build path to include it using the "add jar" button (not the "add external jar", since I'm lead to believe that will use absolute path) of the "configure build path" dialogue. I also added the Bundle-ClassPath line to my manifest file. In the IDE, all my imports resolve fine (because it's in the build path), but when I export it as a bundle (default options), the error log shows that the imports could not be resolved.

I checked the final bundle JAR file that was generated, and the third-party JAR is nested inside (at the root) as expected, but as soon as it tries to import objects from that library, it fails.

Included below is my (very basic) MANIFEST.MF. Am I missing something? Do I need to list it as an import-package in addition to listing it on the bundle-classpath? Do I need to export it (why?)? Am I mis-using bundle-classpath? Do I need to do something differently because I'm using Eclipse?

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: ZWave Demo
Bundle-SymbolicName: com.wbarlow.zwavedemo
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: com.wbarlow.zwavedemo.internal.Activator
Bundle-Vendor: WBARLOW
Bundle-RequiredExecutionEnvironment: J2SE-1.5
Import-Package: org.osgi.framework;version="1.3.0"
Bundle-ClassPath: .,
 rxtx-2.1.7.jar

EDIT: I solved this by re-doing it all using the GUI rather than editing the .MF file directly. I guess Eclipse sets some background configuration when you do that.

like image 946
Woodrow Barlow Avatar asked Nov 11 '14 17:11

Woodrow Barlow


1 Answers

Rather than putting the .JAR in the project's src folder and directly editing the manifest file to refer to it as if it is in the root (even though it really was in the root of the bundle, apparently this didn't work), I was able to solve this problem by using Eclipse's graphical manifest editor.

  1. Put the external JAR in the Eclispe project's root.
  2. Open the META-INF/MANIFEST.MF file in Eclipse.
  3. On the "Runtime" tab, choose "Add..." and add your external JAR.
like image 102
Woodrow Barlow Avatar answered Sep 22 '22 13:09

Woodrow Barlow