【问题标题】:getBundleContext method is undefined issuegetBundleContext 方法是未定义的问题
【发布时间】:2015-07-22 19:28:30
【问题描述】:

我正在尝试设置一个在内部运行在 OSGi 上的应用程序,并尝试使用教程 here,但我一直收到错误消息“方法 getBundleContext() 未针对类型框架定义”。据我所知,我使用的是正确的库,但在提到的文章中没有指定,所以我不是 100% 确定。我还尝试了 Apache 网站上的示例,here,这导致了同样的问题。代码如下:

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.launch.Framework;
import org.osgi.framework.launch.FrameworkFactory;

public class Main {
    public static void main(String[] args) throws BundleException {
        FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
        Map<String, String> config = new HashMap<String, String>();
        Framework framework = frameworkFactory.newFramework(config);
        framework.start();

        // Throws error that it cannot find method getBundleContext()
        BundleContext context = framework.getBundleContext();
        List<Bundle> installedBundles = new LinkedList<Bundle>();

        installedBundles.add(context.installBundle("file:org.apache.felix.shell-1.4.2.jar"));
        installedBundles.add(context.installBundle("file:org.apache.felix.shell.tui-1.4.1.jar"));

        for (Bundle bundle : installedBundles) {
            bundle.start();
        }
    }
}

唯一有意义的是,要么我使用了错误的库,要么库已更改,而我尝试调用的方法在过去 4 年中已被弃用。有谁知道我该如何解决这个问题?

我怀疑它有什么不同,但如果有,我正在使用 Bndtools for Eclipse 来创建这个项目。

【问题讨论】:

    标签: java frameworks osgi


    【解决方案1】:

    发现问题。显然,在 Bndtools 的项目构建路径中导入的 osgi.core 已过期,从而阻止代码访问正确版本的框架库。更新解决了这个问题。

    补充说明;由于我使用的是 Bndtools,因此我通过 bnd.bnd 文件的构建选项卡将其添加到项目构建路径中。然而,这并没有获取osgi.core 的正确版本,所以我必须在源代码下添加version=latest 以强制它获取可用的最新版本,所以该行现在显示为:osgi.core;version=latest以前只是 osgi.core-buildpath: 部分下。

    【讨论】:

    • 在你的构建路径上拥有两个版本的库可能是不明智的。
    • 是的,我错过了一个简单的错误。他们在文件上也有不同的名字,所以我一开始忽略了它。 Bndtools 版本被称为osgi.core,而较新的版本实际上被命名为org.apache.felix.framework
    • 这不仅仅是一个不同的名称,它是一个不同的库,它只是有重叠的内容。 Felix 是一种 OSGi 实现,而osgi.core 是由 OSGi 联盟发布的纯 API。我强烈建议针对 osgi.core 构建...你只需要获得它的正确版本。
    • 很高兴知道。谢谢你的提示。我将寻找它的更新版本,并再次将 felix 库替换为核心库。还要编辑我的答案,以防其他人由于遇到此问题而看到此问题并且没有阅读 cmets。关于 OSGi,我还有很多东西要学,但我现在玩得很开心,因为事情开始形成的速度比他们回来时要快得多,那时我只知道它做了什么,而不是它是如何做到的.啊,自学的乐趣……
    猜你喜欢
    • 2023-01-07
    • 1970-01-01
    • 2011-07-22
    • 2012-03-16
    • 1970-01-01
    • 2014-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多