简体   繁体   English

无法将jar添加到OSGi包中

[英]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". 为此,我使用“jersey-client-1.14.jar”。 That works quite well by adding it to the eclipse projects class path. 通过将其添加到eclipse项目类路径,这非常有效。

Now I am trying to do the same in an OSGi bundle. 现在我试图在OSGi包中做同样的事情。 These were the steps I did: 这些是我做的步骤:

  1. I created a /lib folder in my OSGi bundle project. 我在OSGi包项目中创建了一个/ lib文件夹。
  2. Added the jersey-client-1.14.jar in that folder. 在该文件夹中添加了jersey-client-1.14.jar。
  3. Added the jar in the MANIFEST.MF in the classpath: Bundle-ClassPath: ., lib/jersey-client-1.14.jar 在类路径的MANIFEST.MF中添加了jar: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. 在Eclipse工作区中,我没有编译错误。

At runtime I have the phenomenon that I get a classdef not found exception when creating the jersey client with Client.create(); 在运行时,我遇到了在使用Client.create()创建jersey客户端时遇到classdef not found异常的现象;

!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. 你还需要将jersey-core添加到bundle的类路径中,因为jersey-client将它作为依赖项。

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 查看找不到的类,找到该类所在的jar,将其添加到bundle类路径中,返回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. 但有一件事:泽西罐子已经准备好了OSGi,所以你可以将jersey-client.jar和jersey-core.jar添加到你的taget平台并导入所需的包。

Looking at your secondary queries I think you just want to know what is going on :-) 看看你的次要查询我想你只想知道发生了什么:-)

OSGi fences JARs to create modules. OSGi 围绕 JAR创建模块。 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. 默认情况下,围栅是不可穿透的,捆绑(jar)之外的类都不可见(能够从中加载类)到内部的类,而外人无法在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. 在你的情况下,一个bundle试图加载com.sun.jersey.spi.service.ServiceFinder,但它会进入fence,因为没有合适的漏洞。

The "holes" in OSGi are packages, these are the shared atoms. OSGi中的“漏洞”是封装,这些是共享原子。 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. Import-Package标头指示您需要从外部世界看到的包,Export-Package标头定义了包对其他包可见的包(以及在哪个版本下)。

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. 显然你不想手动计算导入,因为这些已经存在于你的类文件中,因此有一个工具bnd(我是作者)可以从maven,ant,gradle等使用。它需要一个食谱并使用适当的元数据计算生成的包。 This tool is extensively supported with the bndtools Eclipse plugin, which is a very nice environment to learn more about OSGi and these issues. bndtools Eclipse插件广泛支持此工具,这是一个非常好的环境,可以了解有关OSGi和这些问题的更多信息。

Sir, in eclipse you have been added different package like. 先生,在eclipse中你已经添加了不同的包装。 com.sun.jersey.spi.service.ServiceFinder . 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. 就像我有一个jar,它依赖于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. 在OSGi中嵌入jar是一种糟糕的风格。 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. 嵌入jar很容易导致类路径问题,因为包可能来自不同的包。 Is there a special reason why you want to embed the jar? 是否有特殊原因要嵌入罐子? Jersey is fully OSGi ready since version 1.2. 自版本1.2以来,Jersey已完全准备好OSGi。 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. 大多数OSGi容器都带有开箱即用的JAX-RS实现 - 是Jersey或者什么不是。 For instance, we're using Apache ServiceMix, which conveniently provides Apache CXF. 例如,我们正在使用Apache ServiceMix,它可以方便地提供Apache CXF。

  • What container are you using? 你用的是什么容器?
  • Do you really have to bundle Jersey with your application? 你真的必须将Jersey与你的应用程序捆绑在一起吗?

If you need to bundle Jersey for whatever reason, can you please provide an example of your manifest file. 如果您因任何原因需要捆绑Jersey,请提供清单文件的示例。

  • Have you considered using the Maven-Bundle-Plugin? 你考虑过使用Maven-Bundle-Plugin吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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