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

Hannes Knutsson :

I am trying to learn OSGI from the bottom up. Starting with making and running a minimal example bundle.

I have set up a simple project using maven and the previously linked example from the apache felix website. This minimal project seems to compile nicely and I can see no problem when inspecting the contents of the resulting .jar file.

I then install the resulting bundled .jar file in apache felix's gogo osgi-terminal tool using felix:install <mybundle.jar>. After that I try starting it using felix:start <mybundle.jar>.

This is when my problem occurs. I get a stacktrace of a org.osgi.framework.BundleException.

So this is the problem I'm trying to fix. I see no apparent problem nor solution that might help or explain why I'm geting this problem.

I'm also using the apache felix framework version 6.0.3 to run my bundle.

So, my question is: Why am I getting these problems, and what can I do to resolve them so that I can get back on track learning OSGI?

I have been searching the great internet for its infinite wisdom considering all things technical, but cannot find anyone having hade the same problem under these same circumstances.

For further information, I'm using Java 12 and the Maven that's built in to IntelliJ Idea Community Edition. I had the same problem using Java 8 with this same setup and have upgraded to 12 just because it felt smart to do so sooner than later.

The OSGI-Activator class, which is the only code in this project so far is as shows below:

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

The Maven pom.xml used to compile and package the results into a bundled .jar file is as follows:

<?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>

The resulting stacktrace of this is as follows:

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
Christian Schneider :

Felix expects a URI when you install the bundle. Try to either use a full file: url.

Alternatively you can download the felix distribution and put your bundle into the bundle dir.

In general be aware that the felix tutorial is a bit outdated and uses some very low level approaches that you would never use in production.

See the answer to this question: Where to find and install org.osgi.framework package?

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=157410&siteId=1