为什么我的 OSGI 示例不会在捆绑启动时打印 "Hello World"?
Why won't my OSGI example print "Hello World" on bundle start?
以 Neil Bartlett 的书 "OSGi in Practice" 为例,我复制了以下代码,它可以启动、停止和检查目录中的包,并且应该在包启动和停止时打印消息:
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 文件和 org.eclipse.osgi_3 一起放在一个名为 "tutorial" 的文件夹中。 5.1.R35x_v20090827.jar(旧版本的 Eclipse Equinox OSGi 实现附带的 OSGi jar 文件)。
我成功编译了教程 java 文件:
javac -cp ".:org.eclipse.osgi_3.5.1.R35x_v20090827.jar" HelloUpdaterActivator.java
然后我将生成的所有 class 文件放入一个 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
您缺少 Bundle-Activator
header 所以它知道调用 that class' 方法。
以 Neil Bartlett 的书 "OSGi in Practice" 为例,我复制了以下代码,它可以启动、停止和检查目录中的包,并且应该在包启动和停止时打印消息:
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 文件和 org.eclipse.osgi_3 一起放在一个名为 "tutorial" 的文件夹中。 5.1.R35x_v20090827.jar(旧版本的 Eclipse Equinox OSGi 实现附带的 OSGi jar 文件)。
我成功编译了教程 java 文件:
javac -cp ".:org.eclipse.osgi_3.5.1.R35x_v20090827.jar" HelloUpdaterActivator.java
然后我将生成的所有 class 文件放入一个 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
您缺少 Bundle-Activator
header 所以它知道调用 that class' 方法。