OSGi Service wrapping a jar

2019-03-04 01:57发布

问题:

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.

回答1:

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.



回答2:

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...



回答3:

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



标签: java osgi