不能把一捆装进费利克斯。我下载了Felix6.0.1,使用
> java -jar bin/felix.jar
____________________________
Welcome to Apache Felix Gogo
g! 我在eclipse中创建了一个MavenProject TestA:
TestA/src/main/java/testa/impl/Activator.java中创建一个类。testa.impl.Activator扩展到org.osgi.framework.BundleActivator。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!我希望有“你好世界”印刷,但什么也没有出现!
我现在很困惑,想看看捆绑是否已经开始了。
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?
发布于 2019-01-16 16:00:02
Hello没有打印在控制台上,因为您创建的jar实际上不是一个包。奇怪的是,felix允许您启动一个在清单中没有任何OSGI相关信息的jar。我不清楚开始这样的捆绑意味着什么?也许课程被出版了,也许他们没有。
大多数java框架扫描jar中的文件,并使用反射或字节码分析来查找相关的类。对于普通框架,扫描jars的开销在启动时只发生一次。OSGI被设计为轻量级的,并且也被用于移动。此外,由于在OSGI包中可能会出现或消失,所以他们设计了一种更有效的方法。OSGI包将元数据存储在清单中。这是一个简单的文本文件,总是位于同一个位置(在jar中):“META/MANIFEST.MF”。如果您使用zip工具检查jar中的文件,您应该会看到如下所示:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: Peter Rader
Build-Jdk: 1.8.0_111对于未来的OSGI故障排除,我建议您检查(并发布)创建的清单。
有效的OSGI清单如下所示:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: Peter Rader
Build-Jdk: 1.8.0_111
Bundle-Name: testa
Bundle-SymbolicName: testa.impl
Bundle-Version: 1.0.0
Bundle-Activator: testa.impl.Activator
Import-Package: org.osgi.framework当felix读取这个清单时,它使用"Bundle- activator“条目来查找激活器(如果有的话)。替换清单,包应该正常部署。
只需创建一个名为“META/MANIFEST.MF”的文本文件,其中包含相对于jar的位置上的文本。接下来,用以下linux命令(或您最喜欢的zip工具)替换jar中的清单文件:
zip testa-0.0.1-SNAPSHOT.jar -u META-INF/*zip命令的预期输出:
updating: META-INF/MANIFEST.MF (deflated 36%)确保使用zip文件打开jar,并检查内容是否已更改。现在,如果您安装并启动包,它应该打印"Hello!“。
虽然这解决了问题,但它并不是一个非常干净的解决方案。
对于如何格式化清单中的条目,jar规范有一些相当奇怪的规则:
最值得注意的是:
No line may be longer than 72 bytes (not characters), in its UTF8-encoded form.
If a value would make the initial line longer than this,
it should be continued on extra lines (each starting with a single SPACE).因此,您不希望手工编辑此文件。正如其他人所指出的,您有各种自动生成OSGI清单的选项。和前面一样,您可以检查生成的jar中的清单,以验证它是否被正确生成。
发布于 2019-01-07 17:42:46
您似乎正在手动创建清单。您应该使用像plugin这样的工具来实现它。由于您手动创建了清单,因此它似乎是错误的。您不导入在代码中使用的包。即org.osgi.framework.
此外,不要从其他人的代码中扩展激活器类,因为通常不能导入它们的实现包。你自己去做吧:
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class TestA implements BundleActivator {https://stackoverflow.com/questions/54072205
复制相似问题