1. 写好继承 BundleActivator 的JAVA 文件,其中 start() 函数是 OSGI 平台调用开始入口。
package com.javaworld.sample.helloworld;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class Activator implements BundleActivator {
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
public void start(BundleContext context) throws Exception {
System.out.println("Hello World!!");
}
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext context) throws Exception {
System.out.println("Goodbye World!!");
}
}
2. 新建一目录,将 org.eclipse.osgi_3.5.1.R35x_v20090827.jar (ECLIPSE的OSGI包),JAVA文件考到该目录下。进入该目录,编译改类。
javac -classpath org.eclipse.osgi_3.5.1.R35x_v20090827.jar com/javaworld/sample/helloworld/Activator.java
3. 在 META-INF 目录下新建 MANIFEST.MF 文件,指定 Bundle-Activator等设置。
Manifest-Version: 1.0
Bundle-Vendor: JAVAWORLD
Bundle-Version: 1.0.0.qualifier
Bundle-Name: HelloWorld
Created-By: 1.6.0_14 (Sun Microsystems Inc.)
Bundle-Activator: com.javaworld.sample.helloworld.Activator
Bundle-ManifestVersion: 2
Import-Package: org.osgi.framework;version="1.3.0"
Bundle-SymbolicName: com.javaworld.sample.HelloWorld
4. 打包
jar -cfm com.javaworld.sample.helloworld.Activator.jar HELLOWORLD.MF com/javaworld/sample/helloworld/Activator.class
5. 在DOS提示符下进入该目录,启动 OSGI
java -jar org.eclipse.osgi_3.5.1.R35x_v20090827.jar -console
6. 安装新建 BUNDLE
osgi> install file:com.javaworld.sample.helloworld.Activator.jar
7. 根据 ID 启动 BUNDLE,如果ID 是 1
osgi> start 1
8. 停止 BUNDLE
osgi> stop 1
9. 卸载 BUNDLE
osgi> uninstall file:com.javaworld.sample.helloworld.Activator.jar