在 Apache Karaf 中强制导入

Force Imports in Apache Karaf

我有一个主要处理接口的包,它使用来自第三方 jar 的工厂来获取它正在使用的接口的实例。 例如,

my-bundle.jar has...

com.oth.itf.Intrface itf = Fctry.getInstance('ABC');

imports-package for com.oth.itf

third-party.jar has..

public static com.oth.itf.Intrface getInstance(String abc) {
   if (...) {
       return new com.oth.impl.ItfInstance();
   }
}

exports-package for com.oth.itf and com.oth.impl

一切正常,捆绑包也已部署,但问题是我正在

ClassNotFoundException for ItfInstance

my-bundle

代码执行时。

我尝试为 com.oth.impl 添加导入包,但无济于事。当我添加

时,事情开始起作用

dummy declaration of com.oth.impl.ItfInstance

我的某些地方-bundle.jar。看起来,只有当我们明确使用它们时,Karaf 才会获取导入。有没有更好的办法?有没有办法强制 Karaf 导入包,即使我们没有明确使用它们?

Neil 是对的,如果没有在代码中使用它就无法导入。有了 Karaf,您就有可能提供帮助。使用命令 bundle:dynamic-import,您可以在 运行 时间向捆绑包添加动态导入。有了这个,你就可以通过 bundle:headers 找到实际需要的导入,你会找到这个包的导入包。拿走那些并将那些缺少的导入添加到您的清单生成中,然后就可以了。

如果您没有 bundle: 选项,请使用此 dev: 命令:

dev:dynamic-import <BundleID>

这允许 Karaf 在运行时导入依赖项。