Apache ServiceMix 5.4.0上的OSGi NoClassDefFoundError

时间:2015-07-28 14:58:32

标签: java osgi bundle osgi-bundle apache-servicemix

我试图将我的osgi包(在Geronimo 3.0.1上运行得很好)部署到ServiceMix。 Bundle A导出com.mycompany.api包,bundle B导入它。 但是我得到了

java.lang.NoClassDefFoundError: Could not initialize class com.mycompany.api.Version

当我从bundle B中的com.mycompany.api.Version类调用静态方法getMajor()时会发生这种情况。来自api包的其他类工作正常,即使使用其他静态方法/字段也是如此。 的 Version.java:

public class Version {
  private static int VERSION_MAJOR = 0;
  private static int VERSION_MINOR = 0;
  private static int VERSION_BUILD = 0;

  static{
      String v = Version.class.getPackage().getImplementationVersion();
      String av[] = v.split("\\.");
      VERSION_MAJOR = Integer.parseInt(av[0]);
      VERSION_MINOR = Integer.parseInt(av[1]);
      VERSION_BUILD = Integer.parseInt(av[2]);
  }

  public static int getMajor(){
      return VERSION_MAJOR;
  }

  public static int getMinor(){
      return VERSION_MINOR;
  }

  public static int getBuild(){
      return VERSION_BUILD;
  }
}

UPD

完整堆栈跟踪:

2015-07-29 07:57:31,884 | ERROR | qtp882005584-238 | BlueprintContainerImpl           | 18 - org.apache.aries.blueprint.core - 1.4.2 | Unable to start blueprint container for bundle com.mycompany.bundleB
org.osgi.service.blueprint.container.ComponentDefinitionException: Unable to initialize bean Intializer
    at org.apache.aries.blueprint.container.BeanRecipe.runBeanProcInit(BeanRecipe.java:714)
    at org.apache.aries.blueprint.container.BeanRecipe.internalCreate2(BeanRecipe.java:824)
    at org.apache.aries.blueprint.container.BeanRecipe.internalCreate(BeanRecipe.java:787)
    at org.apache.aries.blueprint.di.AbstractRecipe$1.call(AbstractRecipe.java:79)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)[:1.7.0_51]
    at org.apache.aries.blueprint.di.AbstractRecipe.create(AbstractRecipe.java:88)
    at org.apache.aries.blueprint.container.BlueprintRepository.createInstances(BlueprintRepository.java:245)
    at org.apache.aries.blueprint.container.BlueprintRepository.createAll(BlueprintRepository.java:183)
    at org.apache.aries.blueprint.container.BlueprintContainerImpl.instantiateEagerComponents(BlueprintContainerImpl.java:682)
    at org.apache.aries.blueprint.container.BlueprintContainerImpl.doRun(BlueprintContainerImpl.java:377)
    at org.apache.aries.blueprint.container.BlueprintContainerImpl.run(BlueprintContainerImpl.java:269)
    at org.apache.aries.blueprint.container.BlueprintExtender.createContainer(BlueprintExtender.java:294)
    at org.apache.aries.blueprint.container.BlueprintExtender.createContainer(BlueprintExtender.java:263)
    at org.apache.aries.blueprint.container.BlueprintExtender.modifiedBundle(BlueprintExtender.java:253)
    at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$Tracked.customizerModified(BundleHookBundleTracker.java:500)
    at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$Tracked.customizerModified(BundleHookBundleTracker.java:433)
    at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$AbstractTracked.track(BundleHookBundleTracker.java:725)
    at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$Tracked.bundleChanged(BundleHookBundleTracker.java:463)
    at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$BundleEventHook.event(BundleHookBundleTracker.java:422)
    at org.apache.felix.framework.util.SecureAction.invokeBundleEventHook(SecureAction.java:1127)
    at org.apache.felix.framework.util.EventDispatcher.createWhitelistFromHooks(EventDispatcher.java:696)
    at org.apache.felix.framework.util.EventDispatcher.fireBundleEvent(EventDispatcher.java:484)
    at org.apache.felix.framework.Felix.fireBundleEvent(Felix.java:4429)
    at org.apache.felix.framework.Felix.startBundle(Felix.java:2100)
    at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:976)
    at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:963)
    at org.apache.felix.webconsole.internal.core.BundlesServlet.doPost(BundlesServlet.java:364)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:595)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
    at org.apache.felix.webconsole.internal.servlet.OsgiManager.service(OsgiManager.java:526)[213:org.apache.karaf.webconsole.console:2.4.1]
    at org.apache.felix.webconsole.internal.servlet.OsgiManager.service(OsgiManager.java:450)[213:org.apache.karaf.webconsole.console:2.4.1]
    at org.apache.felix.webconsole.internal.KarafOsgiManager.doService(KarafOsgiManager.java:74)[213:org.apache.karaf.webconsole.console:2.4.1]
    at org.apache.felix.webconsole.internal.KarafOsgiManager$1.run(KarafOsgiManager.java:54)
    at java.security.AccessController.doPrivileged(Native Method)[:1.7.0_51]
    at org.apache.karaf.jaas.modules.JaasHelper.doAs(JaasHelper.java:67)[20:org.apache.karaf.jaas.modules:2.4.1]
    at org.apache.felix.webconsole.internal.KarafOsgiManager.service(KarafOsgiManager.java:52)[213:org.apache.karaf.webconsole.console:2.4.1]
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:684)[64:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:503)[64:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
    at org.ops4j.pax.web.service.jetty.internal.HttpServiceServletHandler.doHandle(HttpServiceServletHandler.java:69)[73:org.ops4j.pax.web.pax-web-jetty:3.1.2]
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)[64:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:557)[64:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)[64:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1086)[64:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
    at org.ops4j.pax.web.service.jetty.internal.HttpServiceContext.doHandle(HttpServiceContext.java:240)[73:org.ops4j.pax.web.pax-web-jetty:3.1.2]
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:429)[64:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)[64:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1020)[64:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)[64:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
    at org.ops4j.pax.web.service.jetty.internal.JettyServerHandlerCollection.handle(JettyServerHandlerCollection.java:77)[73:org.ops4j.pax.web.pax-web-jetty:3.1.2]
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)[64:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
    at org.eclipse.jetty.server.Server.handle(Server.java:370)[64:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
    at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:494)[64:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
    at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:982)[64:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
    at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:1043)[64:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
    at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:865)[64:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
    at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:240)[64:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
    at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)[64:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:696)[64:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:53)[64:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)[64:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)[64:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
    at java.lang.Thread.run(Thread.java:744)[:1.7.0_51]
Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.mycompany.api.Version
    at com.mycompany.server.Version.checkAPIVersion(Version.java:69)
    at com.mycompany.Initializer.checkVersion(Initializer.java:53)
    at com.mycompany.Initializer.init(Initializer.java:31)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)[:1.7.0_51]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)[:1.7.0_51]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.7.0_51]
    at java.lang.reflect.Method.invoke(Method.java:606)[:1.7.0_51]
    at org.apache.aries.blueprint.utils.ReflectionUtils.invoke(ReflectionUtils.java:297)
    at org.apache.aries.blueprint.container.BeanRecipe.invoke(BeanRecipe.java:958)
    at org.apache.aries.blueprint.container.BeanRecipe.runBeanProcInit(BeanRecipe.java:712)
    ... 61 more

com.mycompany.server.Version的第69行

if (com.mycompany.api.Version.getMajor() < API_VERSION_MAJOR){

1 个答案:

答案 0 :(得分:0)

问题在于

Version.class.getPackage().getImplementationVersion()

它是null,所以

String av[] = v.split("\\.");

导致nullpointer异常,由于某种原因没有记录。

我之前没有提到,但我的包实际上是api.jar的包装器,它有以下结构:

api-osgi.jar
  --META-INF/MANIFEST.MF
  --WEB-INF/lib/api.jar/META-INF/MANIFEST.MF

&#34;实施-版&#34;只是在api.jar的清单里面,Geronimo也没关系。 将其添加到OSGi包的清单中,修复了服务组合的问题。

谢谢你们的帮助。