为什么在使用 Apache Felix Gogo 启动我的最小示例包时会出现 BundleException?

Why am I getting a BundleException when starting my minimal example bundle with Apache Felix Gogo?

我正在尝试自下而上地学习 OSGI。从制作和 运行ning minimal example bundle.

开始

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

然后我使用 felix:install <mybundle.jar> 在 apache felix 的 gogo osgi-terminal 工具中安装生成的捆绑 .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 class,目前项目中唯一的代码,如下所示:

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.");
        }
    }
}

Mavenpom.xml用于将结果编译打包成一个bundled.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

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

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

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

查看此问题的答案: