Embedding JARs in an OSGi Bundle in Eclipse
Asked Answered
B

2

6

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.

Bak answered 11/11, 2014 at 17:14 Comment(2)
Don't feel bad about embedding a JAR, it is perfectly ok for the reasons you indicate. You make your bundle a lot easier to use by embedding these tiny implementation dependencies (and hiding them!). Using the Bundle-ClassPath is unfortunately not so nice but unless you use bnd/Bndtools it is hard to just include the appropriate classes.Chalone
Eclipse caches the MANIFEST.MF. So editing it has mostly no effect until you restart Eclipse :/Afterguard
B
4

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.
Bak answered 11/11, 2014 at 17:14 Comment(0)
P
0

You need an 'Export-Package' entry to say which packages are available to other plugins.

You can specify this in the Eclipse MANIFEST.MF editor on the 'Runtime' tab, 'Exported Packages' table.

Penguin answered 11/11, 2014 at 17:21 Comment(3)
i thought export-package was used when i want other bundles to be able to reference things from my bundle. i'm only using the third-party JAR internally, so i don't need to expose it to other bundles, why would it need to be exported?Bak
thank you for the assistance. i did try doing this (I added the third-party JAR's namespace to the exported packages table) and it did not solve the problem.Bak
i found a workaround (see my edit; eclipse was being a pain). sorry for wasting your time, and thank you for your assistance.Bak

© 2022 - 2024 — McMap. All rights reserved.