osgiapache-karafosgi-bundle

Apache Karaf / Felix does not log System.out.println


I am trying to get started with OSGi and want to run a simple hello world app. I followed this tutorial here: https://www.baeldung.com/osgi.

I am able to create a bundle in karaf and run it but unfortunately the System.out.println logs are not shown. What am I doing wrong?

Karaf console:

karaf@root()> bundle:list
START LEVEL 100 , List Threshold: 50
ID │ State    │ Lvl │ Version        │ Name
───┼──────────┼─────┼────────────────┼───────────────────────────────────────────────────────────────────────────────
32 │ Active   │  80 │ 4.4.3          │ Apache Karaf :: OSGi Services :: Event
62 │ Resolved │  80 │ 1.0.0.SNAPSHOT │ my-app
karaf@root()> bundle:start 62
karaf@root()>

App.java

package de.tom.osgi;

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

public class App implements BundleActivator
{
    @Override
    public void start(BundleContext context) throws Exception {
        // TODO Auto-generated method stub
        System.out.println("Hello App: start");
    }
    @Override
    public void stop(BundleContext context) throws Exception {
        // TODO Auto-generated method stub
        System.out.println("Hello App: stop");
    }
}

pom.xml

<?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">
  <modelVersion>4.0.0</modelVersion>

  <groupId>de.tom.osgi</groupId>
  <artifactId>my-app</artifactId>
  <packaging>bundle</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-app</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.release>17</maven.compiler.release>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.osgi</groupId>
      <artifactId>org.osgi.core</artifactId>
      <version>6.0.0</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>3.1.0</version>
      </plugin>
      <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>3.0.2</version>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
      </plugin>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.1</version>
      </plugin>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.0.2</version>
      </plugin>
      <plugin>
        <artifactId>maven-install-plugin</artifactId>
        <version>2.5.2</version>
      </plugin>
      <plugin>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.8.2</version>
      </plugin>
      <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
      <plugin>
        <artifactId>maven-site-plugin</artifactId>
        <version>3.7.1</version>
      </plugin>
      <plugin>
        <artifactId>maven-project-info-reports-plugin</artifactId>
        <version>3.0.0</version>
      </plugin>
      <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <version>5.1.8</version>
        <extensions>true</extensions>
        <configuration>
            <instructions>
              <Private-Package>de.tom.osgi</Private-Package>
              <Bunde-Activator>de.tom.osgi.App</Bunde-Activator>
            </instructions>
          </configuration>
      </plugin>
    </plugins>
</project>

Solution

  • The problem was a typo in the pom.xml. I wrote Bunde-Activator instead of Bundle-Activator. After fixing this everything works now. However I got no error message or anything and just found out through trial-and-error.