以Neil Bartlett的书“OSGi in Practice”为例,我复制了以下代码,它可以启动,停止和检查目录中的bundle,并且应该在bundle start和stop上打印消息:
package tutorial;
import java.io.File;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
public class HelloUpdaterActivator implements BundleActivator {
// ...
private final Thread thread = new Thread(new BundleUpdater());
private volatile BundleContext context;
@Override
public void start(BundleContext context) throws Exception {
System.out.println("Hello World");
this.context = context;
thread.start();
}
@Override
public void stop(BundleContext context) throws Exception {
System.out.println("Goodbye World");
thread.interrupt();
}
protected Bundle findBundleByLocation(String location) {
// ... Not relevant to lack of any print statements in OSGi console.
}
private class BundleUpdater implements Runnable {
@Override
public void run() {
// ... Not relevant to lack of any print statements in OSGi console.
}
}
^我把它放在一个名为HelloUpdaterActivator.java的文件中,并将该java文件放在一个名为“tutorial”的文件夹中,同时放入org.eclipse.osgi_3.5.1.R35x_v20090827.jar(OSGi的jar文件随旧版一起提供) OSGi的Eclipse Equinox实现版本)。
我用:
成功编译了教程java文件javac -cp ".:org.eclipse.osgi_3.5.1.R35x_v20090827.jar" HelloUpdaterActivator.java
然后我获取了生成的所有类文件并将它们放入一个类似的jar中:
jar cf HelloUpdaterActivator.jar ./HelloUpdaterActivator.class ./HelloUpdaterActivator\$BundleUpdater.class ./HelloUpdaterActivator\$1.class
最后,我为jar创建了一个通用样本MANIFEST.MF文件,如下所示:
Manifest-Version: 1.0
Created-By: 1.7.0_79 (Oracle Corporation)
Bundle−Name: OSGi Bundle
Bundle−Symbolic Name: example1
Bundle−Version: 1.0.1
Bundle−Required Execution Environment: J2SE−1.6
现在jar已经准备就绪,我在终端中打开了OSGi,它有自己的小控制台,如下所示:
java -jar ./org.esgi_3.5.1.R35x_v20090827.jar -console -configuration runtime
OSGi控制台看起来像这样:
osgi>
我输入OSGi控制台:
osgi> install file:HelloUpdaterActivator.jar
Bundle id is 1
osgi> ss
Framework is launched.
id State Bundle
0 ACTIVE org.eclipse.osgi_3.5.1.R35x_v20090827
1 INSTALLED unknown_0.0.0 [1]
osgi> start 1
osgi> ss
Framework is launched.
id State Bundle
0 ACTIVE org.eclipse.osgi_3.5.1.R35x_v20090827
1 ACTIVE unknown_0.0.0 [1]
osgi> stop 1
osgi> ss
Framework is launched.
id State Bundle
0 ACTIVE org.eclipse.osgi_3.5.1.R35x_v20090827
1 RESOLVED unknown_0.0.0 [1]
但即使它说我制作的jar文件是活动的,“Hello World”永远不会在开始时打印,而“Goodbye World”永远不会打印停止。为什么我的OSGi教程不会在捆绑启动时打印“Hello World”或在捆绑停止时打印“Goodbye world”?
更新
我通过将MANIFEST.MF文件更改为:
来实现它Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: OSGi_Bundle - Activator
Bundle-Version: 1.0.0.SNAPSHOT
Bundle-Activator: Activator
Bundle-ClassPath: .
Bundle-Description: Activator
Bundle-SymbolicName: activator
Import-Package: org.osgi.framework
答案 0 :(得分:2)
您缺少Bundle-Activator标头,因此它知道调用您的类'方法。