首页 > 解决方案 > 为什么在使用 Apache Felix Gogo 启动我的最小示例包时会出现 BundleException?

问题描述

我正在尝试自下而上地学习 OSGI。从制作和运行一个最小的示例包开始。

我已经使用 maven 和 apache felix 网站上之前链接的示例建立了一个简单的项目。这个最小的项目似乎编译得很好,在检查生成的 .jar 文件的内容时我看不到任何问题。

然后,我使用 .jar 在 apache felix 的 gogo osgi-terminal 工具中安装生成的捆绑 .jar 文件felix:install <mybundle.jar>。之后我尝试使用felix:start <mybundle.jar>.

这是我的问题发生的时候。我得到一个 org.osgi.framework.BundleException 的堆栈跟踪。

所以这就是我要解决的问题。我看不到任何明显的问题或解决方案可能有助于或解释我遇到此问题的原因。

我还使用 apache felix 框架版本 6.0.3 来运行我的包。

所以,我的问题是:为什么我会遇到这些问题,我可以做些什么来解决这些问题,以便我可以回到学习 OSGI 的轨道上?

考虑到所有技术问题,我一直在寻找伟大的互联网以寻找它的无限智慧,但找不到任何人在同样的情况下遇到同样的问题。

有关更多信息,我正在使用 Java 12 和 IntelliJ Idea Community Edition 中内置的 Maven。我在使用相同设置的 Java 8 时遇到了同样的问题,并且已经升级到 12 只是因为早晚这样做感觉很聪明。

OSGI-Activator 类是本项目目前唯一的代码,如下所示:

package com.github.hannesknutsson.privatepkg;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;

public class Activator implements BundleActivator, ServiceListener {

    private BundleContext context;

    public void start(BundleContext bundleContext) throws Exception {
        context = bundleContext;

        System.out.println("Starting to listen for service events.");
        context.addServiceListener(this);
    }

    public void stop(BundleContext bundleContext) throws Exception {
        System.out.println("Stopped listening for service events.");
    }

    public void serviceChanged(ServiceEvent serviceEvent) {
        String[] objectClass = (String[])
                serviceEvent.getServiceReference().getProperty("objectClass");

        if (serviceEvent.getType() == ServiceEvent.REGISTERED)
        {
            System.out.println(
                    "Ex1: Service of type " + objectClass[0] + " registered.");
        }
        else if (serviceEvent.getType() == ServiceEvent.UNREGISTERING)
        {
            System.out.println(
                    "Ex1: Service of type " + objectClass[0] + " unregistered.");
        }
        else if (serviceEvent.getType() == ServiceEvent.MODIFIED)
        {
            System.out.println(
                    "Ex1: Service of type " + objectClass[0] + " modified.");
        }
    }
}

Maven pom.xml 用于将结果编译并打包成一个捆绑的 .jar 文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>
        <artifactId>testproject</artifactId>
        <groupId>com.github.hannesknutsson</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <packaging>bundle</packaging>
    <artifactId>TestBundle</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
                    </archive>
                </configuration>
                <version>3.1.1</version>
            </plugin>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Export-Package>${project.groupId}.services</Export-Package>
                        <Private-Package>${project.groupId}.privatepkg</Private-Package>
                        <Bundle-Activator>${project.groupId}.privatepkg.Activator</Bundle-Activator>
                    </instructions>
                </configuration>
                <version>4.2.0</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>12</source>
                    <target>12</target>
                </configuration>
                <version>3.8.1</version>
            </plugin>
        </plugins>
    </build>

</project>

生成的堆栈跟踪如下:

org.osgi.framework.BundleException: Unable to cache bundle: TestBundle-1.0-SNAPSHOT.jar
        at org.apache.felix.framework.Felix.installBundle(Felix.java:3231)
        at org.apache.felix.framework.BundleContextImpl.installBundle(BundleContextImpl.java:147)
        at org.apache.felix.framework.BundleContextImpl.installBundle(BundleContextImpl.java:120)
        at org.apache.felix.gogo.command.Basic.start(Basic.java:734)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:567)
        at org.apache.felix.gogo.runtime.Reflective.invoke(Reflective.java:139)
        at org.apache.felix.gogo.runtime.CommandProxy.execute(CommandProxy.java:91)
        at org.apache.felix.gogo.runtime.Closure.executeCmd(Closure.java:599)
        at org.apache.felix.gogo.runtime.Closure.executeStatement(Closure.java:526)
        at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:415)
        at org.apache.felix.gogo.runtime.Pipe.doCall(Pipe.java:416)
        at org.apache.felix.gogo.runtime.Pipe.call(Pipe.java:229)
        at org.apache.felix.gogo.runtime.Pipe.call(Pipe.java:59)
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
        at java.base/java.lang.Thread.run(Thread.java:835)
Caused by: java.net.MalformedURLException: no protocol: TestBundle-1.0-SNAPSHOT.jar
        at java.base/java.net.URL.<init>(URL.java:650)
        at org.apache.felix.framework.util.SecureAction.createURL(SecureAction.java:256)
        at org.apache.felix.framework.cache.JarRevision.initialize(JarRevision.java:148)
        at org.apache.felix.framework.cache.JarRevision.<init>(JarRevision.java:76)
        at org.apache.felix.framework.cache.BundleArchive.createRevisionFromLocation(BundleArchive.java:799)
        at org.apache.felix.framework.cache.BundleArchive.reviseInternal(BundleArchive.java:480)
        at org.apache.felix.framework.cache.BundleArchive.<init>(BundleArchive.java:148)
        at org.apache.felix.framework.cache.BundleCache.create(BundleCache.java:462)
        at org.apache.felix.framework.Felix.installBundle(Felix.java:3227)
        ... 19 more
java.net.MalformedURLException: no protocol: TestBundle-1.0-SNAPSHOT.jar

标签: javaosgiapache-felixosgi-bundle

解决方案


Felix 在安装包时需要一个 URI。尝试使用完整文件: url.

或者,您可以下载 felix 发行版并将您的捆绑包放入捆绑包目录。

一般来说,请注意 felix 教程有点过时,并且使用了一些您永远不会在生产中使用的非常低级的方法。

查看这个问题的答案:在哪里可以找到并安装 org.osgi.framework 包?


推荐阅读