我必须修改现有的应用程序作为OSGI插件,我偶然发现了以下问题。这个应用程序依赖于jar
文件中的xml
文件,这个应用程序也在运行时加载这个文件,因为在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
那么让我们假设template.xml位于一个jar文件resources.jar中的resources/template.xml。这个jar在你的bundle中,使用bundle - classpath来引用。
在这种情况下,资源jar的内容应该添加到bundle的类路径中。
所以当你在bundle的一个类中你应该可以这样做:
this.getClass().getClassLoader().getResourceAsStream("resources/template.xml")
如果这不起作用,那么也许你需要以"/"开始路径,但我认为没有