简体   繁体   English

运行OSGi包中包含的jar

[英]Running a jar included in an OSGi bundle

The situation is that I need to package a runnable jar, X.jar into an OSGi bundle. 情况是我需要将一个可运行的jar,X.jar打包成一个OSGi包。 The jar itself can not be modified and OSGi is mandatory as X.jar is to be distributed and run via a software program that requires OSGi bundles. jar本身无法修改,OSGi是强制性的,因为X.jar将通过需要OSGi包的软件程序进行分发和运行。 The application contained in X.jar is to communicate over a network with another application. X.jar中包含的应用程序是通过网络与另一个应用程序进行通信。

My plan was to create an OSGi bundle with eclipse and then include X.jar in it. 我的计划是用eclipse创建一个OSGi包,然后在其中包含X.jar。 But the problem is that X.jar is not detected. 但问题是没有检测到X.jar。 If I instead try to access a copy of X.jar on my file system it works. 如果我试图在我的文件系统上访问X.jar的副本,它可以工作。 My bundle file structure: 我的捆绑文件结构:

 -\MyBundle
   -\META-INF
     -\MANIFEST.MF
   -\src
     -\mybundle
       -\Activator.class
   -\X.jar

Activator: 激活:

 public void start(BundleContext context)  {
    Thread XThread = new Thread(){
        public void run(){
            try {
                 Runtime.getRuntime().exec("java -jar X.jar");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                       e.printStackTrace();
            }
        }
      };
 }

Manifest.MF 的Manifest.MF

 Manifest-Version: 1.0
 Bundle-ManifestVersion: 2
 Bundle-Name: Bundle
 Bundle-SymbolicName: myBundle
 Bundle-Version: 1.0.0.qualifier
 Bundle-Activator: com.ericsson.mas.Activator
 Bundle-ClassPath: .,X.jar
 Bundle-Vendor: BundleVendor
 Bundle-RequiredExecutionEnvironment: JavaSE-1.7
 Import-Package: org.osgi.framework;version="1.3.0"

I suspect that there is some problem with my Bundle-ClassPath, but it seems correct when I search around. 我怀疑我的Bundle-ClassPath存在一些问题,但是当我搜索时它似乎是正确的。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

Note: I am aware that this goes completely against OSGi's philosophy. 注意:我知道这完全违背了OSGi的理念。

If you want to run it as an external java process, you will need to extract the jar from your bundle and point to the extracted jar. 如果要将其作为外部java进程运行,则需要从bundle中提取jar并指向提取的jar。 Your bundle data file storage is a good place to extract the bundle. 捆绑数据文件存储是提取捆绑包的好地方。 Putting the jar on the class path only helps if you want to load the classes directly in your activator. 将jar放在类路径上只有在想要直接在激活器中加载类时才有用。

You can also run it by calling the declared main class directly, no need to extract it and run another java process. 您也可以通过直接调用声明的主类来运行它,无需提取它并运行另一个java进程。 So take a look in the X.jars manifest, ther should be a Main declared. 所以看看X.jars清单,应该是Main声明的。 then in your activator, simply call: 然后在你的激活器中,只需拨打:

the.package.from.x.MainClassX.main(new String[] {});

inside your thread. 在你的线程内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM