Apache ServiceMix 5.4.0 上的 OSGi NoClassDefFoundError

OSGi NoClassDefFoundError on Apache ServiceMix 5.4.0

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

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

当我从 com.mycompany.api.Version class 中的 bundle B 调用静态方法 getMajor() 时会发生这种情况。来自 api 包的其他 classes 工作正常,甚至与其他静态 methods/fields。 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;
  }
}

更新

完整堆栈跟踪:

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

第 69 行 com.mycompany.server.Version

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

问题出在

Version.class.getPackage().getImplementationVersion()

它是空的,所以

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

导致空指针异常,由于某种原因未记录。

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

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

"Implementation-Version" 仅在 api.jar 的清单中,对于 Geronimo 来说没问题。 将其添加到 OSGi 包的清单中,解决了服务组合的问题。

感谢你们的帮助。