項目建好了,我們來看一下MANIFEST.MF 這個文件
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: MyFirstEquinoxExample Plug-in
Bundle-SymbolicName: myFirstEquinoxExample
Bundle-Version: 1.0.0
Bundle-Activator: myfirstequinoxexample.Activator
Import-Package: org.osgi.framework;version="1.3.0"
Eclipse-LazyStart: true
說明一下 Import-Package: org.osgi.framework;version="1.3.0" 這句話的意思是要運行這個例子,必須先導入 版本號爲 "1.3.0"的org.osgi.framework包,
我們來看下Activator.java這個類:
package myfirstequinoxexample;
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!!");
}
}
看見沒,這個類只有兩個方法,一個是 start() ,一個是 stop(),其中 start方法是在插件啓動/加載的時候調用的,而stop()是在停止/卸載的時候調用的,我們來加點屬於自己的東西進去
package myfirstequinoxexample;
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("這是我第一個OSGI的例子,已經成功啓動了,一切運行正常");
}
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext context) throws Exception {
System.out.println("已經成功將這個插件停止/卸載了");
}
}
我們來運行它看看效果,選中項目,右鍵單擊 ,從彈出菜單中選擇 "Run As" --> "Open Run Dialog",
打開配置窗口,按照圖片中標的序號來(我CS玩的不是很好,畫的很難看)
第一步:
雙擊 OSGi Framework,新建一個運行配置 ,
第二步:
將運行配置名稱改成 myFirstEquinoxExample 點擊 Apply ,修改生效了
第三步:
單擊 Deselect All,全部取消選擇,
第四步:
選擇我們要啓動的項目 myFirstEquinoxExample(1.0.0),然後在單擊 Add Required Bundles
第五步:
點擊 Run ,OK 啓動了