简体   繁体   English

OSGI插件访问resources.jar中的文件

[英]OSGI plugin access the file in the resources.jar

I have to modify existing application to act as an OSGI plugin, I have stumbled on the following issue.我必须修改现有应用程序以充当 OSGI 插件,我偶然发现了以下问题。 This application depends on the xml file which is in the jar file, this application also loads this file during the runtime, and because of that in the OSGI environment it is not accessible.此应用程序依赖于jar文件中的xml文件,此应用程序也在运行时加载此文件,因此在 OSGI 环境中它是不可访问的。 Any suggestion how I can fix this issue, so far I have tried following solution none have yield any positive results yet.关于如何解决这个问题的任何建议,到目前为止,我已经尝试了以下解决方案,但没有产生任何积极的结果。

This is how initially it was loaded这是它最初的加载方式

InputStream templateInputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("resources/template.xml");

I tried to do it in the following way我尝试通过以下方式做到这一点

URL url = null;
try {
  url = new URL("file:/lib/resources.jar");
} catch (MalformedURLException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
URL[] urls = {url};
URLClassLoader classLoader = new URLClassLoader(urls, DeclareMapFactory.class.getClassLoader());
Thread.currentThread().setContextClassLoader(classLoader);
InputStream templateInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("resources/template.xml");

This was also an attempt这也是一次尝试

InputStream templateInputStream = this.getClass().getClassLoader().getResourceAsStream("resources/template.xml");

Jar file is in the lib directory and is loaded via the MANIFEST.MF Jar 文件位于lib目录中,并通过MANIFEST.MF加载

So lets assume template.xml is located at resources/template.xml in a jar resources.jar.因此,让我们假设 template.xml 位于 jar resources.jar 中的 resources/template.xml。 This jar is inside your bundle and referenced using Bundle-ClassPath.这个 jar 位于您的包中,并使用 Bundle-ClassPath 进行引用。

In this case the content of the resources jar should be added to the classpath of your bundle.在这种情况下,资源 jar 的内容应该添加到包的类路径中。

So when you are inside a class of the bundle you should be able to do:因此,当您在捆绑包的类中时,您应该能够执行以下操作:

this.getClass().getClassLoader().getResourceAsStream("resources/template.xml")

If that does not work then maybe you need to start the path with a "/" but I think it should work without.如果这不起作用,那么也许您需要以“/”开头,但我认为它应该可以工作。

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

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