以 Neil Bartlett 的书“OSGi in Practice”为例,我复制了以下代码,该代码可以启动、停止和检查目录中的 bundle ,并且应该在 bundle 启动和停止时打印消息:
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.5.1.R35x_v20090827.jar 一起放在名为“tutorial”的文件夹中(旧版本附带的 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 文件处于 Activity 状态,“Hello World”也不会在启动时打印,“Goodbye World”也不会在停止时打印。为什么我的 OSGi 教程不会在 bundle 启动时打印“Hello World”或在 bundle 停止时打印“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 ,因此它知道调用该类的方法。
关于java - 为什么我的 OSGI 示例不会在 bundle 启动时打印 "Hello World"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30040468/