tags:

views:

346

answers:

3

Hello all,

I am trying to create an OSGi service that wraps another jar. I added the jar to the project, the classpath and the binary build. I registered the service in the Activator but when the consuming bundle calls the service I get a java.lang.NoClassDefFoundError on the wrapper jar. Does anyone have any idea what I am doing wrong here?

Thanks in advance.

A: 

Did you use Bundle-Classpath in the manifest? Why aren't you using bnd for an existing jar?

Yes, I am using Bundle-Classpath. I am not using bnd as this is a demo app - it is more for instructional than actual use. I'd like to know why the problem is as opposed to just pressing on. Is this due to the separate classpaths e.g. the consumer can not see the providers embedded lib jar? Something else?
javamonkey79
A: 

There can be multiple reasons for your behavior. To make sure, I would check for the following:

  • assuming you work with Eclipse check if you have included the jar in your "Build" tab of the manifest editor, as well as pointed to this very jar within the "Runtime" tab under "Classpath".

  • the created bundle: does it contain the jar? Does it have the "Bundle-ClassPath" header pointing to the jar, like: "Bundle-ClassPath: lib/myLibrary.jar,." (the last . is required to include the classes coming from the root directory of the bundle - your activator f.i.)

  • make sure, the jar actually contains all required dependencies or expresses them via Import-Package headers in the wrapping bundle. Eclipse has a "Import Wizard" for just that. The before mentioned bnd tool does the same by the way. Hope that helps...

Mirko Jahn
I was not trying it by building all the way, I was just running from Eclipse. However, when I packaged it the lib jar is in the bundle and this was in the manifest:Bundle-ClassPath: .,lib/the_jar_that_is_failing.jarI ran install file on the bundle and updated the consumer jar. At this point the consumer jar was not started. When I try and start it I get the exception. I know about BND, but I was hoping to learn what I am doing wrong as opposed to 'just fixing it' :)
javamonkey79
+2  A: 

Are you exporting the packages that are required by the consumer, as well as all those that the implementation requires. The consumer will need to import everything that will be referenced.

As a side note, creating a bundle this way doesn't work well in Eclipse for development (works fine for runtime). If you try to reference a class or interface in the jar from another OSGi project, the IDE won't resolve anything since it cannot 'see' the files in the jar. The jar has to be expanded within the bundle for everything to be visible (within the IDE). Eclipse automagically creates the appropriate classpath references based on the imports and exports for build purposes. Without the jar file in the bundle, you will have to explicitly maintain this classpath.

Robin
I've used lib jars in OSGi bundles before, just not with a service delegating to them. I think I get it now - thanks much!
javamonkey79