1. 項目pom.xml的<Import-Package>中添加以下兩個包:
org.eclipse.osgi.framework.console,
org.osgi.framework
2. implements CommandProvider接口,並實現(Override)該接口的getHelp()方法。該方法會在OSGi控制檯的help命令中添加提示信息。
3. 需要作爲命令的方法,方法簽名格式如下:
public void _<command_name>(CommandInterpreter ci)
command_name會作爲命令添加到OSGi控制檯中,參數通過ci傳入。
4. 添加下面這個方法,並在start()方法中調用它
private void registerWithOSGIConsole() {
BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
bundleContext.registerService(CommandProvider.class.getName(), this, null);
}