我必须修改现有的应用程序以充当OSGI插件,我偶然发现了以下问题。此应用程序依赖于xml
文件中的jar
文件,此应用程序还会在运行时加载此文件,因此在OSGI环境中无法访问该文件。任何建议我如何解决这个问题,到目前为止我已经尝试过以下解决方案没有产生任何积极的结果呢。
这是最初加载的方式
InputStream templateInputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("resources/template.xml");
我尝试按以下方式执行此操作
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");
这也是一次尝试
InputStream templateInputStream = this.getClass().getClassLoader().getResourceAsStream("resources/template.xml");
Jar文件位于lib
目录中,并通过MANIFEST.MF
答案 0 :(得分:2)
因此,假设template.xml位于jar资源.jar中的resources / template.xml。这个jar在你的bundle里面,并使用Bundle-ClassPath引用。
在这种情况下,资源jar的内容应该添加到bundle的类路径中。
所以当你进入一个类的一个类时,你应该能够做到:
this.getClass().getClassLoader().getResourceAsStream("resources/template.xml")
如果这不起作用,那么你可能需要用" /"来启动路径。但我认为它应该没有。