在eclipse中配置OSGI运行环境运行环境
在eclipse中搭建运行环境,eclipse的版本很关键,也正如OSGI的文档中所描述的那样。
既然是基于 Equinox,自然要先下 Equinox,但由于 Equinox 是 Eclipse 的工程,而且
Eclipse 3.1 以后的版本都是通过它来启动的,这也就意味在 Eclipse 3.1 以后的版本其
实本身就包含了 Equinox 的,所以如果你采用的是 Eclipse 3.1 以后的版本,那么就省
事 了 , 在 你 的 Eclipse 的 plugins 目 录 下 你 可 以 找 到 类 似
org.eclipse.osgi_3.2.0.v20060510.jar 这样的文件,它其实就是 Equinox 的 OSGI R4 Core
Framework 的实现了,如果你采用的不是 Eclipse 3.1 以后的版本,由于我们在开发基
于 Equinox 的应用时采用的也是 Eclipse,那么还是建议你去下个 Eclipse 3.1 以后的版
本。
小编这里使用的eclipse版本是eclipse_luna,如图所示:
配置OSGI环境
接下来就开始配置osgi的环境了。
按照OSGI开发文档中的描述,还需要添加两个jar
- org.eclipse.equinox.http_1.0.0.v20060601a.jar
- org.eclipse.equinox.servlet.api_1.0.0.v20060601.jar
书中给的下载地址:http://download.eclipse.org/eclipse/equinox/drops/S-3.2RC7-200606021317/index.php
但是小编去访问这个链接时,指向的是一个不存在的地址Not found,于是重新在度娘中找了下资源,找到一个可以访问的下载地址http://archive.eclipse.org/eclipse/equinox/drops/R-3.2-200606291905/index.php。
将这两个jar下载好之后,放到eclipse的plugins目录中,重启eclipse即可。
- 新建一个osgi的运行环境,点击Run旁的小三角,如图所示:
- 先在左边的OSGi Framework右键new一个新的环境出来,然后在Bundles这个tab页,添加如图所示的bundle。 如果你的eclipse中没有osgi framework这个选项,建议你直接去下载一个高版本的eclipse。
- 在Arguments这个tab页添加如图所示的参数,小编这里是默认就有的参数。!!!注意!!!:经过小编的测试,-Declipse.ignoreApp=true和**-Dosgi.noShutdown=true这两个运行参数很重要,缺少就会运行报错,特别是-Dosgi.noShutdown=true**这个参数。
- 点击Apply保存配置,点击Run运行当前配置的OSGI环境。但是小编在按照OSGI的文档添加了以上的jar包后,运行的时候就出现了一下的错误。现在咱们来一个一个解决。
运行异常
异常一:Could not resolve module: org.eclipse.equinox.console
异常描述
详细异常日志如下:
!SESSION 2020-06-28 09:25:52.017 -----------------------------------------------
eclipse.buildId=unknown
java.version=1.8.0_191
java.vendor=Oracle Corporation
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=zh_CN
Command-line arguments: -dev file:D:/workspace/osgi/.metadata/.plugins/org.eclipse.pde.core/stanard/dev.properties -os win32 -ws win32 -arch x86_64 -consoleLog -console
!ENTRY org.eclipse.equinox.console 4 0 2020-06-28 09:25:52.562
!MESSAGE FrameworkEvent ERROR
!STACK 0
org.osgi.framework.BundleException: Could not resolve module: org.eclipse.equinox.console [4]
Unresolved requirement: Import-Package: org.apache.felix.service.command; version="0.8.0"; status="provisional"
at org.eclipse.osgi.container.Module.start(Module.java:434)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.incStartLevel(ModuleContainer.java:1582)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.incStartLevel(ModuleContainer.java:1562)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.doContainerStartLevel(ModuleContainer.java:1533)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.dispatchEvent(ModuleContainer.java:1476)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.dispatchEvent(ModuleContainer.java:1)
at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:340)
!ENTRY org.eclipse.osgi 4 0 2020-06-28 09:25:52.564
!MESSAGE Bundle initial@reference:file:plugins/org.eclipse.equinox.console_1.1.0.v20140131-1639.jar was not resolved.
!ENTRY org.eclipse.osgi 4 0 2020-06-28 09:25:52.567
!MESSAGE Could not start bundle: org.eclipse.equinox.console
!STACK 0
org.osgi.framework.BundleException: Could not start bundle: org.eclipse.equinox.console
at org.eclipse.core.runtime.internal.adaptor.ConsoleManager.checkForConsoleBundle(ConsoleManager.java:64)
at org.eclipse.core.runtime.adaptor.EclipseStarter.startup(EclipseStarter.java:331)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:231)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:648)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:603)
at org.eclipse.equinox.launcher.Main.run(Main.java:1465)
at org.eclipse.equinox.launcher.Main.main(Main.java:1438)
Caused by: org.osgi.framework.BundleException: Could not resolve module: org.eclipse.equinox.console [4]
Unresolved requirement: Import-Package: org.apache.felix.service.command; version="0.8.0"; status="provisional"
at org.eclipse.osgi.container.Module.start(Module.java:434)
at org.eclipse.osgi.internal.framework.EquinoxBundle.start(EquinoxBundle.java:393)
at org.eclipse.core.runtime.internal.adaptor.ConsoleManager.checkForConsoleBundle(ConsoleManager.java:62)
... 10 more
Root exception:
org.osgi.framework.BundleException: Could not resolve module: org.eclipse.equinox.console [4]
Unresolved requirement: Import-Package: org.apache.felix.service.command; version="0.8.0"; status="provisional"
at org.eclipse.osgi.container.Module.start(Module.java:434)
at org.eclipse.osgi.internal.framework.EquinoxBundle.start(EquinoxBundle.java:393)
at org.eclipse.core.runtime.internal.adaptor.ConsoleManager.checkForConsoleBundle(ConsoleManager.java:62)
at org.eclipse.core.runtime.adaptor.EclipseStarter.startup(EclipseStarter.java:331)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:231)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:648)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:603)
at org.eclipse.equinox.launcher.Main.run(Main.java:1465)
at org.eclipse.equinox.launcher.Main.main(Main.java:1438)
截图如下:
解决方案:
字面意思是不能解析org.eclipse.equinox.console,那我们就去添加这个bundle试试看。
这个时候再点击运行,小编出现了另一个异常,我们先看看异常信息再想解决办法。
异常二:
异常描述:
详细异常信息如下:
!SESSION 2020-06-28 10:06:23.021 -----------------------------------------------
eclipse.buildId=unknown
java.version=1.8.0_191
java.vendor=Oracle Corporation
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=zh_CN
Command-line arguments: -dev file:D:/workspace/osgi/.metadata/.plugins/org.eclipse.pde.core/stanard/dev.properties -os win32 -ws win32 -arch x86_64 -consoleLog -console
!ENTRY org.eclipse.equinox.console 4 0 2020-06-28 10:06:23.596
!MESSAGE FrameworkEvent ERROR
!STACK 0
org.osgi.framework.BundleException: Could not resolve module: org.eclipse.equinox.console [4]
Unresolved requirement: Import-Package: org.apache.felix.service.command; version="0.8.0"; status="provisional"
at org.eclipse.osgi.container.Module.start(Module.java:434)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.incStartLevel(ModuleContainer.java:1582)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.incStartLevel(ModuleContainer.java:1562)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.doContainerStartLevel(ModuleContainer.java:1533)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.dispatchEvent(ModuleContainer.java:1476)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.dispatchEvent(ModuleContainer.java:1)
at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:340)
!ENTRY org.eclipse.osgi 4 0 2020-06-28 10:06:23.598
!MESSAGE Bundle initial@reference:file:plugins/org.eclipse.equinox.console_1.1.0.v20140131-1639.jar was not resolved.
!ENTRY org.eclipse.osgi 4 0 2020-06-28 10:06:23.600
!MESSAGE Could not start bundle: org.eclipse.equinox.console
!STACK 0
org.osgi.framework.BundleException: Could not start bundle: org.eclipse.equinox.console
at org.eclipse.core.runtime.internal.adaptor.ConsoleManager.checkForConsoleBundle(ConsoleManager.java:64)
at org.eclipse.core.runtime.adaptor.EclipseStarter.startup(EclipseStarter.java:331)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:231)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:648)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:603)
at org.eclipse.equinox.launcher.Main.run(Main.java:1465)
at org.eclipse.equinox.launcher.Main.main(Main.java:1438)
Caused by: org.osgi.framework.BundleException: Could not resolve module: org.eclipse.equinox.console [4]
Unresolved requirement: Import-Package: org.apache.felix.service.command; version="0.8.0"; status="provisional"
at org.eclipse.osgi.container.Module.start(Module.java:434)
at org.eclipse.osgi.internal.framework.EquinoxBundle.start(EquinoxBundle.java:393)
at org.eclipse.core.runtime.internal.adaptor.ConsoleManager.checkForConsoleBundle(ConsoleManager.java:62)
... 10 more
Root exception:
org.osgi.framework.BundleException: Could not resolve module: org.eclipse.equinox.console [4]
Unresolved requirement: Import-Package: org.apache.felix.service.command; version="0.8.0"; status="provisional"
at org.eclipse.osgi.container.Module.start(Module.java:434)
at org.eclipse.osgi.internal.framework.EquinoxBundle.start(EquinoxBundle.java:393)
at org.eclipse.core.runtime.internal.adaptor.ConsoleManager.checkForConsoleBundle(ConsoleManager.java:62)
at org.eclipse.core.runtime.adaptor.EclipseStarter.startup(EclipseStarter.java:331)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:231)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:648)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:603)
at org.eclipse.equinox.launcher.Main.run(Main.java:1465)
at org.eclipse.equinox.launcher.Main.main(Main.java:1438)
截图如下:
解决方案
一样的Could not resolve module错误,那我们再去添加对应的bundle试试。添加bundle的方式同上一步骤。
不过我们这里按照org.apache.felix.service.command这个去搜索bundle的时候发现,是找不到这个bundle的,那我们就尝试搜索下org.apache.felix,这个时候会搜索出来三个bundle,如下所示:
经过小编的测试,以上截图中的bundle缺一不可,少任何一个都会报错,这里我们就先全部添加进去,继续往下走,先把环境运行起来。
再Run一下试试,OK,不负众望,OSGI环境已经运行起来了,我们先敲个OSGI的命令ss
看看在当前osgi framework中运行了哪些bundle,如下图所示:
异常补充
今天由于本地运行了nginx,占用了80端口,再运行osgi的时候,出现了下面这个异常
#异常三
异常描述
详细错误日志如下:
!SESSION 2020-06-28 09:36:33.997 -----------------------------------------------
eclipse.buildId=unknown
java.version=1.8.0_191
java.vendor=Oracle Corporation
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=zh_CN
Command-line arguments: -dev file:D:/workspace/osgi/.metadata/.plugins/org.eclipse.pde.core/stanard/dev.properties -os win32 -ws win32 -arch x86_64 -consoleLog -console
!ENTRY org.eclipse.equinox.http 4 0 2020-06-28 09:36:34.577
!MESSAGE Port 80 is use
!STACK 0
java.net.BindException: Address already in use: JVM_Bind
at java.net.DualStackPlainSocketImpl.bind0(Native Method)
at java.net.DualStackPlainSocketImpl.socketBind(DualStackPlainSocketImpl.java:106)
at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:387)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:190)
at java.net.ServerSocket.bind(ServerSocket.java:375)
at java.net.ServerSocket.<init>(ServerSocket.java:237)
at org.eclipse.equinox.http.HttpServerSocket.<init>(HttpServerSocket.java:48)
at org.eclipse.equinox.http.HttpConfiguration.createServerSocket(HttpConfiguration.java:220)
at org.eclipse.equinox.http.HttpListener.setProperties(HttpListener.java:108)
at org.eclipse.equinox.http.HttpListener.<init>(HttpListener.java:55)
at org.eclipse.equinox.http.HttpConfiguration.createDefaultListeners(HttpConfiguration.java:138)
at org.eclipse.equinox.http.HttpConfiguration.initialize(HttpConfiguration.java:102)
at org.eclipse.equinox.http.HttpConfiguration.<init>(HttpConfiguration.java:90)
at org.eclipse.equinox.http.Http.start(Http.java:70)
at org.eclipse.equinox.http.Activator.start(Activator.java:33)
at org.eclipse.osgi.internal.framework.BundleContextImpl$3.run(BundleContextImpl.java:771)
at org.eclipse.osgi.internal.framework.BundleContextImpl$3.run(BundleContextImpl.java:1)
at java.security.AccessController.doPrivileged(Native Method)
at org.eclipse.osgi.internal.framework.BundleContextImpl.startActivator(BundleContextImpl.java:764)
at org.eclipse.osgi.internal.framework.BundleContextImpl.start(BundleContextImpl.java:721)
at org.eclipse.osgi.internal.framework.EquinoxBundle.startWorker0(EquinoxBundle.java:936)
at org.eclipse.osgi.internal.framework.EquinoxBundle$EquinoxModule.startWorker(EquinoxBundle.java:319)
at org.eclipse.osgi.container.Module.doStart(Module.java:571)
at org.eclipse.osgi.container.Module.start(Module.java:439)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.incStartLevel(ModuleContainer.java:1582)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.incStartLevel(ModuleContainer.java:1562)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.doContainerStartLevel(ModuleContainer.java:1533)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.dispatchEvent(ModuleContainer.java:1476)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.dispatchEvent(ModuleContainer.java:1)
at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:340)
!ENTRY org.eclipse.equinox.http 4 0 2020-06-28 09:36:34.579
!MESSAGE An unexpected IOException occurred
!STACK 0
java.net.BindException: Address already in use: JVM_Bind
at java.net.DualStackPlainSocketImpl.bind0(Native Method)
at java.net.DualStackPlainSocketImpl.socketBind(DualStackPlainSocketImpl.java:106)
at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:387)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:190)
at java.net.ServerSocket.bind(ServerSocket.java:375)
at java.net.ServerSocket.<init>(ServerSocket.java:237)
at org.eclipse.equinox.http.HttpServerSocket.<init>(HttpServerSocket.java:48)
at org.eclipse.equinox.http.HttpConfiguration.createServerSocket(HttpConfiguration.java:220)
at org.eclipse.equinox.http.HttpListener.setProperties(HttpListener.java:108)
at org.eclipse.equinox.http.HttpListener.<init>(HttpListener.java:55)
at org.eclipse.equinox.http.HttpConfiguration.createDefaultListeners(HttpConfiguration.java:138)
at org.eclipse.equinox.http.HttpConfiguration.initialize(HttpConfiguration.java:102)
at org.eclipse.equinox.http.HttpConfiguration.<init>(HttpConfiguration.java:90)
at org.eclipse.equinox.http.Http.start(Http.java:70)
at org.eclipse.equinox.http.Activator.start(Activator.java:33)
at org.eclipse.osgi.internal.framework.BundleContextImpl$3.run(BundleContextImpl.java:771)
at org.eclipse.osgi.internal.framework.BundleContextImpl$3.run(BundleContextImpl.java:1)
at java.security.AccessController.doPrivileged(Native Method)
at org.eclipse.osgi.internal.framework.BundleContextImpl.startActivator(BundleContextImpl.java:764)
at org.eclipse.osgi.internal.framework.BundleContextImpl.start(BundleContextImpl.java:721)
at org.eclipse.osgi.internal.framework.EquinoxBundle.startWorker0(EquinoxBundle.java:936)
at org.eclipse.osgi.internal.framework.EquinoxBundle$EquinoxModule.startWorker(EquinoxBundle.java:319)
at org.eclipse.osgi.container.Module.doStart(Module.java:571)
at org.eclipse.osgi.container.Module.start(Module.java:439)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.incStartLevel(ModuleContainer.java:1582)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.incStartLevel(ModuleContainer.java:1562)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.doContainerStartLevel(ModuleContainer.java:1533)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.dispatchEvent(ModuleContainer.java:1476)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.dispatchEvent(ModuleContainer.java:1)
at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:340)
!ENTRY org.eclipse.equinox.http 4 0 2020-06-28 09:36:34.581
!MESSAGE An unexpected RuntimeException occurred
!STACK 0
java.net.BindException: Address already in use: JVM_Bind
at java.net.DualStackPlainSocketImpl.bind0(Native Method)
at java.net.DualStackPlainSocketImpl.socketBind(DualStackPlainSocketImpl.java:106)
at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:387)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:190)
at java.net.ServerSocket.bind(ServerSocket.java:375)
at java.net.ServerSocket.<init>(ServerSocket.java:237)
at org.eclipse.equinox.http.HttpServerSocket.<init>(HttpServerSocket.java:48)
at org.eclipse.equinox.http.HttpConfiguration.createServerSocket(HttpConfiguration.java:220)
at org.eclipse.equinox.http.HttpListener.setProperties(HttpListener.java:108)
at org.eclipse.equinox.http.HttpListener.<init>(HttpListener.java:55)
at org.eclipse.equinox.http.HttpConfiguration.createDefaultListeners(HttpConfiguration.java:138)
at org.eclipse.equinox.http.HttpConfiguration.initialize(HttpConfiguration.java:102)
at org.eclipse.equinox.http.HttpConfiguration.<init>(HttpConfiguration.java:90)
at org.eclipse.equinox.http.Http.start(Http.java:70)
at org.eclipse.equinox.http.Activator.start(Activator.java:33)
at org.eclipse.osgi.internal.framework.BundleContextImpl$3.run(BundleContextImpl.java:771)
at org.eclipse.osgi.internal.framework.BundleContextImpl$3.run(BundleContextImpl.java:1)
at java.security.AccessController.doPrivileged(Native Method)
at org.eclipse.osgi.internal.framework.BundleContextImpl.startActivator(BundleContextImpl.java:764)
at org.eclipse.osgi.internal.framework.BundleContextImpl.start(BundleContextImpl.java:721)
at org.eclipse.osgi.internal.framework.EquinoxBundle.startWorker0(EquinoxBundle.java:936)
at org.eclipse.osgi.internal.framework.EquinoxBundle$EquinoxModule.startWorker(EquinoxBundle.java:319)
at org.eclipse.osgi.container.Module.doStart(Module.java:571)
at org.eclipse.osgi.container.Module.start(Module.java:439)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.incStartLevel(ModuleContainer.java:1582)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.incStartLevel(ModuleContainer.java:1562)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.doContainerStartLevel(ModuleContainer.java:1533)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.dispatchEvent(ModuleContainer.java:1476)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.dispatchEvent(ModuleContainer.java:1)
at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:340)
osgi>
截图如下:
解决方案
这个异常就好比我们tomcat的8080端口被占用了一样,我们重新指定下端口即可,通过配置**-Dorg.osgi.service.http.port=6666**,如图所示:
添加完配置后,再运行下即可。