简体   繁体   English

使用另一个OSGi捆绑包中的类

[英]Use class from another OSGi bundle

I'm running in a strange issue with OSGi bundle. 我在OSGi捆绑软件中遇到了一个奇怪的问题。

I've got two bundle, let's say B1 and B2 . 我有两个捆绑包,比如说B1B2 B1 export the package abc which contain a class name Foo with a method: B1使用以下方法导出包含类名称Foo的包abc

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

Then B2 import B1 package and try to use the class Foo from B1 . 然后B2导入B1包,并尝试使用B1 Foo类。 I have no problem at compilation, neither when starting the bundle in Felix, everything seem to work fine. 我在编译时没有问题,在Felix中启动捆绑包时,一切似乎都工作正常。

BUT when B2 is call to use the helloWorld method, B2 stop working and do not throw any error ! 但是,当调用B2使用helloWorld方法时, B2停止工作并且不引发任何错误! After some debug I figure the return type of the method helloWorld is the cause, if I change JsonNode to String everything work as expected. 经过一些调试后,我发现方法helloWorld的返回类型是原因,如果我将JsonNode更改为String一切正常。

Why I haven't error in my felix console ? 为什么我的felix控制台没有出错? Why I can't call the helloWorld method when the return type is JsonNode ? 为什么返回类型为JsonNode时我不能调用helloWorld方法?

Thanks for help ! 感谢帮助 !

Edit1: More information, the following method does the exact same bug: Edit1:更多信息,以下方法执行完全相同的错误:

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

I see in the console the Hello World! 我在控制台中看到了Hello World! Then nothing, no error, no trace, just like if the program choose to stop here and wait ! 然后什么也没有,没有错误,没有跟踪,就像程序选择在此处停止并等待!

Edit2: I have this warning when building, I don't know if it's important: Edit2: 构建时出现此警告,我不知道这是否重要:

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

Warning remove, still got the same comportement. 警告删除,仍然得到相同的称呼。

Edit3: I manage to get an error in my console and I think i'm closed to find out what's the problem !!! Edit3:我设法在控制台中出现错误,我认为我已经关闭以找出问题所在!

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)

I guess you call the method in a BundleActivator. 我猜您在BundleActivator中调用了该方法。 A common problem there is that exceptions in eg the start method are not logged. 常见的问题是未记录例如start方法中的异常。 So try to use try catch around the call to helloworld and print or log the exception. 因此,请尝试使用try catch处理对helloworld的调用,并打印或记录异常。

I think the problem with JsonNode is that bundle B2 does not see the class or B1 sees a different class. 我认为JsonNode的问题在于,捆绑包B2看不到该类,或者B1看到了一个不同的类。 Make sure that both bundles import the package JsonNode is in. 确保两个捆绑包都导入了JsonNode包。

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

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