有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Felix未启动捆绑包

无法将包裹装入felix。我下载了Felix 6.0.1,使用

> java -jar bin/felix.jar
____________________________
Welcome to Apache Felix Gogo

g! 

我在eclipse中创建了一个MavenProject TestA

  1. 我向felix(6.0.1)添加了一个依赖项
  2. 我在TestA/src/main/java/testa/impl/Activator.java中创建了一个类
  3. 我将类testa.impl.Activator扩展为org.osgi.framework.BundleActivator
  4. 我覆盖public void start(BundleContext bc) throws Exception以打印出Hello World!

以下是java源代码:

package testa.impl;

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

public class Activator implements BundleActivator {

    public void start(BundleContext arg0) throws Exception {
        System.out.println("Hello World!");
    }

    public void stop(BundleContext arg0) throws Exception {
        System.out.println("stop");
    }
}

这是我的pom.xml

<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>test</groupId>
    <artifactId>testa</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>org.apache.felix.main</artifactId>
            <version>6.0.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <defaultGoal>clean install org.apache.felix:maven-bundle-plugin:bundle</defaultGoal>
    </build>
</project>

然后我使用mvn编译到jar,并使用

g! install file:/C:/xxx/TestA/target/testa-0.0.1-SNAPSHOT.jar                              
Bundle ID: 20

然后我使用lb列出所有包

g! lb                                                                                                           15:51:56
START LEVEL 1
   ID|State      |Level|Name
    0|Active     |    0|System Bundle (6.0.1)|6.0.1
    1|Active     |    1|jansi (1.17.1)|1.17.1
    2|Active     |    1|JLine Bundle (3.7.0)|3.7.0
    3|Active     |    1|Apache Felix Bundle Repository (2.0.10)|2.0.10
    4|Active     |    1|Apache Felix Gogo Command (1.0.2)|1.0.2
    5|Active     |    1|Apache Felix Gogo JLine Shell (1.1.0)|1.1.0
    6|Active     |    1|Apache Felix Gogo Runtime (1.1.0)|1.1.0
   20|Installed  |    1|testa (0.0.1.SNAPSHOT)|0.0.1.SNAPSHOT
g!                                                                                                              

无论如何,我使用start启动包:

g! start 20
g!

我本想打印“Hello World”,但什么也没出现

我现在很困惑,想看看捆绑是否真的开始了

g! lb                                                                                                           15:51:56
START LEVEL 1
   ID|State      |Level|Name
    0|Active     |    0|System Bundle (6.0.1)|6.0.1
    1|Active     |    1|jansi (1.17.1)|1.17.1
    2|Active     |    1|JLine Bundle (3.7.0)|3.7.0
    3|Active     |    1|Apache Felix Bundle Repository (2.0.10)|2.0.10
    4|Active     |    1|Apache Felix Gogo Command (1.0.2)|1.0.2
    5|Active     |    1|Apache Felix Gogo JLine Shell (1.1.0)|1.1.0
    6|Active     |    1|Apache Felix Gogo Runtime (1.1.0)|1.1.0
   20|Active     |    1|testa (0.0.1.SNAPSHOT)|0.0.1.SNAPSHOT
g!                                                                                                              15:51:58

它已启动,但我的代码尚未执行

问题:

为什么控制台上没有打印Hello World


共 (1) 个答案

  1. # 1 楼答案

    您似乎正在手动创建清单。你应该使用像bnd maven plugin这样的工具来实现这一点。由于您手动创建了清单,因此它似乎是错误的。不导入代码中使用的包。即org.osgi.framework

    另外,不要从其他人的代码中扩展activator类,因为您通常无法导入他们的实现包。你自己动手吧:

    import org.osgi.framework.BundleActivator;
    import org.osgi.framework.BundleContext;
    
    public class TestA implements BundleActivator {