【问题标题】:Programmatically start an OSGi folder bundle以编程方式启动 OSGi 文件夹包
【发布时间】:2015-04-17 19:23:06
【问题描述】:

我在 Eclipse 中创建了一个 OSGi Bundle 项目,我想从另一个 Java 项目以编程方式启动它。

这就是我想要做的:

FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
Map<String,String> config = new HashMap<String,String>();
Framework framework = frameworkFactory.newFramework(config);
framework.start();
BundleContext context = framework.getBundleContext();
Bundle bundle = context.installBundle("file:./test.ServiceA");
bundle.start();

但我有这个错误

Exception in thread "main" org.osgi.framework.BundleException: Error loading bundle activator.
...
Caused by: java.lang.ClassNotFoundException: test.servicea.Activator cannot be found by test.ServiceA_1.0.0.qualifier

如果我安装插件 jar 而不是指向文件夹,一切正常

Bundle bundle = context.installBundle("file:./tmp/test.ServiceA_1.0.0.201504172035.jar");       

但我想直接调试Eclipse项目文件夹。

谁能帮帮我?

【问题讨论】:

    标签: java eclipse osgi equinox


    【解决方案1】:

    将“reference:”放在 URL 的开头。通过这样做

    • 捆绑包的内容不会复制到 OSGi 容器的工作(或缓存)文件夹,但会从其原始位置使用
    • 文件夹可以作为 OSGi 包安装

    这不是规范的一部分,但适用于 Equinox 和 Felix(可能也适用于 Knopflerfish)

    根据您的示例:

    Bundle bundle = context.installBundle("reference:file:./test.ServiceA");
    

    基于 cmets 编辑

    由于 MANIFEST.MF 文件在 ./test.ServiceA 中,但编译后的类在 ./test.serviceA/bin 下,因此 "Bundle -Classpath: bin" MANIFEST 标头必须临时提供,直到从文件夹安装包。在发布之前,必须删除标头,因为类将位于生成的 JAR 的根目录中。

    【讨论】:

    • 它不起作用,它继续返回相同的错误java.lang.ClassNotFoundException: test.servicea.Activator cannot be found by test.ServiceA_1.0.0.qualifier
    • ./test.serviceA/test/servicea/Activator.class 是否存在?您能否尝试使用绝对路径安装您的包?
    • Eclipse 将所有类放在./test.serviceA/bin/... 文件夹下。
    • 在这种情况下,唯一的机会是您将“Bundle-Classpath”MANIFEST 标头临时添加到您的捆绑包中,并告知这些类位于捆绑包的 bin 文件夹中。在发布之前,您必须删除此标头,因为在这种情况下您的 jar 将无法工作。 WAB 使用相同的技术来判断类在 WEB-INF/classes 下。更多信息:wiki.osgi.org/wiki/Bundle-ClassPath
    • WAB 是打包为 OSGi 包的 WAR。我以使用 Bundle-Classpath 清单标头的示例为例。您应该尝试添加“Bundle-Classpath:bin”。
    猜你喜欢
    • 2011-06-08
    • 2014-01-12
    • 2017-06-02
    • 2015-02-06
    • 1970-01-01
    • 2013-12-28
    • 2013-08-17
    • 2018-07-29
    相关资源
    最近更新 更多