【问题标题】:Use class from another OSGi bundle使用来自另一个 OSGi 包的类
【发布时间】:2015-01-21 10:43:55
【问题描述】:

我在使用 OSGi 包时遇到了一个奇怪的问题。

我有两个包,比如说B1B2B1 导出包a.b.c,其中包含一个类名Foo 和一个方法:

public JsonNode helloWorld() {
    System.out.println("Hello World!");
    return null;
}

然后B2导入B1包并尝试使用B1中的Foo类。我在编译时没有问题,在 Felix 中启动包时,一切似乎都正常。

但是当调用B2 以使用helloWorld 方法时,B2 停止工作并且不抛出任何错误! 经过一些调试,我认为方法 helloWorld 的返回类型是原因,如果我将 JsonNode 更改为 String 一切都按预期工作。

为什么我的 felix 控制台没有错误?为什么返回类型为 JsonNode 时无法调用helloWorld 方法?

感谢您的帮助!

Edit1:更多信息,以下方法会出现完全相同的错误:

 public String helloWorld() {
     System.out.println("Hello World!");
     JsonNode test =  JsonNodeFactory.instance.objectNode();
     return test.asText();
 }

我在控制台中看到 Hello World!然后什么都没有,没有错误,没有痕迹,就像程序选择在这里停下来等待一样!

编辑2: 构建的时候有这个警告,不知道是不是很重要:

[WARNING] Bundle fr.aaa.ccc.bbbb:1.0.0 : Export javax.json,  has 1,  private references [javax.json.stream], 

警告删除,仍然有相同的行为。

编辑3: 我设法在我的控制台中得到一个错误,我想我已经关闭以找出问题所在了!!!

Caused by: java.lang.LinkageError: 
loader constraint violation: 
when resolving interface method "a.b.c.FooInterface.welcome(Ljava/lang/String;)Lorg/codehaus/jackson/JsonNode;"
the class loader (instance of org/apache/felix/framework/BundleWiringImpl$BundleClassLoaderJava5) 
of the current class, d/e/f/lasthope/Activator, 
and the class loader (instance of org/apache/felix/framework/BundleWiringImpl$BundleClassLoaderJava5) for the method's defining class, a/b/c/aa/bb/FooInterface,
have different Class objects for the type org/codehaus/jackson/JsonNode used in the signature
    at d.e.f.Activator.start(Activator.java:37)

【问题讨论】:

  • 请发布 StackTrace。
  • 正如所说,根本没有堆栈跟踪。我在调用 helloWorld 方法之前和之后放置了一条日志消息。第一条日志消息出现在控制台中,但第二条从未出现。没有错误,没有堆栈跟踪,什么都没有>

标签: java export osgi bundle


【解决方案1】:

我猜你在 BundleActivator 中调用了该方法。一个常见的问题是,例如启动方法未记录。因此,请尝试在对 helloworld 的调用中使用 try catch 并打印或记录异常。

我认为 JsonNode 的问题在于捆绑 B2 看不到类或 B1 看到不同的类。确保两个包都导入了 JsonNode 所在的包。

【讨论】:

  • 我已经有一个 try catch 块围绕着 System.out 的 helloWorld 调用,printStackTrace 并登录 catch 块以确保我会知道我是否到达那里。我的两个捆绑包具有依赖项 javax.json,我验证,两者在“Maven 依赖项”中获得了相同的库名称和版本。
  • Manifest 是否有用于 JsonNode 包的 Import-Package?确保您没有嵌入 Json 类。如果您在某个地方为您的捆绑软件提供代码,这将有很大帮助。
猜你喜欢
  • 1970-01-01
  • 2013-11-27
  • 2018-06-12
  • 2016-07-22
  • 2018-06-13
  • 1970-01-01
  • 2011-04-12
  • 2018-02-12
  • 2014-11-02
相关资源
最近更新 更多