简体   繁体   中英

Unable to add jar to OSGi bundle

Yet another silly question. I am sure it is something easy, but that already took me a lot of time and I don't get it working ;(

I have a written a working prototype to make a rest call to a server. To do this I utilize "jersey-client-1.14.jar". That works quite well by adding it to the eclipse projects class path.

Now I am trying to do the same in an OSGi bundle. These were the steps I did:

  1. I created a /lib folder in my OSGi bundle project.
  2. Added the jersey-client-1.14.jar in that folder.
  3. Added the jar in the MANIFEST.MF in the classpath: Bundle-ClassPath: ., lib/jersey-client-1.14.jar
  4. Checked that it was also correctly added to the projects classpath.
  5. In the Eclipse workspace I have no compiling errors.

At runtime I have the phenomenon that I get a classdef not found exception when creating the jersey client with Client.create();

!ENTRY org.eclipse.equinox.event 4 0 2012-11-08 23:14:43.975

!MESSAGE Exception while dispatching event org.osgi.service.event.Event [topic=openhab/command/Hue_Bulb_2] to handler org.openhab.binding.hue.internal.HueBinding@70f5f42b

!STACK 0

java.lang.NoClassDefFoundError: Could not initialize class com.sun.jersey.spi.service.ServiceFinder

at com.sun.jersey.api.client.Client.init(Client.java:213)

at com.sun.jersey.api.client.Client.access$000(Client.java:118)

at com.sun.jersey.api.client.Client$1.f(Client.java:191)

at com.sun.jersey.api.client.Client$1.f(Client.java:187)

at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193)

at com.sun.jersey.api.client.Client.(Client.java:187)

at com.sun.jersey.api.client.Client.(Client.java:159)

at com.sun.jersey.api.client.Client.create(Client.java:669)

at org.openhab.binding.hue.internal.bridge.HueBridge.getSettingsJson(HueBridge.java:64)

at org.openhab.binding.hue.internal.bridge.HueBridge.pairBridgeIfNecessary(HueBridge.java:19)

at org.openhab.binding.hue.internal.HueBinding.receiveCommand(HueBinding.java:37)

at org.openhab.core.events.AbstractEventSubscriber.handleEvent(AbstractEventSubscriber.java:62)

at org.eclipse.equinox.internal.event.EventHandlerWrapper.handleEvent(EventHandlerWrapper.java:197)

at org.eclipse.equinox.internal.event.EventHandlerTracker.dispatchEvent(EventHandlerTracker.java:197)

at org.eclipse.equinox.internal.event.EventHandlerTracker.dispatchEvent(EventHandlerTracker.java:1)

at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)

at org.eclipse.osgi.framework.eventmgr.ListenerQueue.dispatchEventSynchronous(ListenerQueue.java:148)

at org.eclipse.equinox.internal.event.EventAdminImpl.dispatchEvent(EventAdminImpl.java:135)

at org.eclipse.equinox.internal.event.EventAdminImpl.sendEvent(EventAdminImpl.java:78)

at org.eclipse.equinox.internal.event.EventComponent.sendEvent(EventComponent.java:39)

at org.openhab.core.internal.events.EventPublisherImpl.sendCommand(EventPublisherImpl.java:76)

at org.openhab.ui.webapp.internal.servlet.CmdServlet.service(CmdServlet.java:115)

at org.eclipse.equinox.http.servlet.internal.ServletRegistration.service(ServletRegistration.java:61)

at org.eclipse.equinox.http.servlet.internal.ProxyServlet.processAlias(ProxyServlet.java:128)

at org.eclipse.equinox.http.servlet.internal.ProxyServlet.service(ProxyServlet.java:60)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)

at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:598)

at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:486)

at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)

at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1065)

at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:413)

at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:192)

at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:999)

at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117)

at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:250)

at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:149)

at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:111)

at org.eclipse.jetty.server.Server.handle(Server.java:350)

at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:454)

at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:890)

at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:944)

at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:630)

at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:230)

at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:77)

at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:606)

at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:46)

at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:603)

at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:538)

at java.lang.Thread.run(Thread.java:680)

23:14:43.992 ERROR OSGi[:98] - Exception while dispatching event org.osgi.service.event.Event [topic=openhab/command/Hue_Bulb_2] to handler org.openhab.binding.hue.internal.HueBinding@70f5f42b

java.lang.NoClassDefFoundError: Could not initialize class com.sun.jersey.spi.service.ServiceFinder

at com.sun.jersey.api.client.Client.init(Client.java:213)

at com.sun.jersey.api.client.Client.access$000(Client.java:118)

at com.sun.jersey.api.client.Client$1.f(Client.java:191)

at com.sun.jersey.api.client.Client$1.f(Client.java:187)

at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193)

at com.sun.jersey.api.client.Client.(Client.java:187)

at com.sun.jersey.api.client.Client.(Client.java:159)

at com.sun.jersey.api.client.Client.create(Client.java:669)

at org.openhab.binding.hue.internal.bridge.HueBridge.getSettingsJson(HueBridge.java:64)

at org.openhab.binding.hue.internal.bridge.HueBridge.pairBridgeIfNecessary(HueBridge.java:19)

at org.openhab.binding.hue.internal.HueBinding.receiveCommand(HueBinding.java:37)

You'll also need to add jersey-core to you bundle's classpath, as jersey-client has it as a dependency.

You might alos have to add other dependencies if needed. The process is rather easy if not pleasant:

  1. Add a jar
  2. Run the app
  3. See what class is not found, find the jar where that class lives, add it to the bundle classpath, go back to 2

One thing though: Jersey jars are OSGi-ready, so you might as well just add jersey-client.jar and jersey-core.jar to your taget platform and import the required packages.

Looking at your secondary queries I think you just want to know what is going on :-)

OSGi fences JARs to create modules. By default, the fence is impenetrable, no classes outside the bundle (the jar) are visible (being able to load a class from) to classes on the inside, and outsiders cannot see anything inside the bundle. The advantage should be obvious: you can change the inside to your hearts delight since nothing is known outside.

However, in real life you need some holes in the fence to be able to collaborate with others. In your case a bundle tries to load com.sun.jersey.spi.service.ServiceFinder but it runs into the fence because there is no appropriate hole.

The "holes" in OSGi are packages, these are the shared atoms. You list these packages in the manifest. The Import-Package header indicates the packages that you need to see from the outside world and the Export-Package header defines what packages (and under what version) the packages are visible to other bundles.

Obviously you do not want to manually calculate the imports since these are already in your class files, for this reason there is a tool bnd (I am the author) that can be used from maven, ant, gradle, etc. It takes a recipe and calculates the resulting bundle with proper metadata. This tool is extensively supported with the bndtools Eclipse plugin, which is a very nice environment to learn more about OSGi and these issues.

Sir, in eclipse you have been added different package like. com.sun.jersey.spi.service.ServiceFinder . to start ur service you have to start other service. on which you bundle is dependent. like i have an jar, which is depependent on rxtxcomm_api-2.1.7.jar. i had to initialize it or you can say intiat it. before my service. please check it.

Embedding jars is a bad style in OSGi. Generally you should only do this if there is no other way. Embedding jars will easily lead to classpath problems as packages then may come from different bundles. Is there a special reason why you want to embed the jar? Jersey is fully OSGi ready since version 1.2. See the documentation for examples how to use it.

Most OSGi containers come with a JAX-RS implementation out-of-the-box - being Jersey or what-not. For instance, we're using Apache ServiceMix, which conveniently provides Apache CXF.

  • What container are you using?
  • Do you really have to bundle Jersey with your application?

If you need to bundle Jersey for whatever reason, can you please provide an example of your manifest file.

  • Have you considered using the Maven-Bundle-Plugin?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM