Apache Karaf中System.out.println日志不显示的问题排查
OSGi Karaf中Hello World Bundle日志不显示问题排查
问题描述
学习OSGi时,创建了my-app Bundle并在Karaf中启动,但System.out.println的日志未在控制台输出。
Karaf控制台输出
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>
问题原因及解决方法
核心错误:Bundle Activator配置拼写错误
在pom.xml的maven-bundle-plugin配置中,<Bunde-Activator>是拼写错误,正确标签应为<Bundle-Activator>。这个错误导致OSGi框架无法识别并加载Activator类,start()方法从未执行,因此没有日志输出。修正后的配置片段:
<instructions> <Private-Package>de.tom.osgi</Private-Package> <Bundle-Activator>de.tom.osgi.App</Bundle-Activator> </instructions>验证步骤
- 修改pom.xml后重新构建Bundle:
mvn clean install - 在Karaf中更新并重启Bundle:
bundle:update 62 bundle:stop 62 bundle:start 62
此时控制台应能看到
Hello App: start的输出。- 修改pom.xml后重新构建Bundle:
额外建议
虽然System.out.println可临时使用,但在OSGi/Karaf环境中更推荐使用标准日志框架(如SLF4J+Logback),能更好地集成Karaf日志系统,便于日志级别控制和管理。
内容的提问来源于stack exchange,提问作者Tom
相关产品推荐
相关产品推荐

