我使用 eclipse 插件项目向导(使用 eclipse Helios)创建了两个 OSGI 包 A 和 B。
在捆绑包 B 的清单文件中,我添加了捆绑包 A 作为依赖项。此外,我已导出 A 中的包,以便它们对 B 可见。我在捆绑包 A 中还有一个 .properties 文件,我希望该文件对捆绑包 B 可见。在捆绑包 A 的 build.properties 窗格中,我指定了:
source.. = src/
bin.includes = META-INF/,\
.,\
bundle_A.properties
现在,在捆绑包 B 中,我尝试使用以下命令加载 .properties 文件:
private Properties loadProperties() {
Properties properties = new Properties();
InputStream istream = this.getClass().getClassLoader().getResourceAsStream(
"bundle_A.properties");
try {
properties.load(istream);
} catch (IOException e) {
logger.error("Properties file not found!", e);
}
return properties;
}
但这会产生空指针异常(在类路径上找不到该文件)。
是否可以从捆绑包 A 导出资源(就像导出包一样)或者以其他方式从 B 访问 A 中的文件(从捆绑包 B 访问捆绑包 A 的类加载器)?
The getEntry(String)
方法上Bundle
就是为了这个目的。您可以使用它从任何捆绑包中加载任何资源。另请参阅方法findEntries()
and getEntryPaths()
如果您不知道捆绑包内资源的确切路径。
不需要获取包的类加载器来执行此操作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)