我正在运行一个基于包的项目(在eclipse rcp中)。在一个特定的bundle中,由于基于jar的库代码,我需要访问一个文件夹。所以在实践中,我的要求是在我的项目中有一个特定的配置文件文件夹。如果我测试我的项目作为一个正常的java应用程序,一切运行顺利,然而,当我试图在osgi中运行它,它失败了,因为它没有找到文件夹。如何将文件夹添加到运行时环境中?我试图添加到包的运行时类路径配置,但这并不能解决我的问题。
我的bundle Manifest。我正试图添加配置/文件夹。
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: net.beaconcontroller.datastore
Bundle-SymbolicName: net.beaconcontroller.datastore
Bundle-Version: 1.0.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Export-Package: net.beaconcontroller.datastore
Bundle-ClassPath: .,
lib/bft-smart-hashserver.jar,
config/
打开清单编辑器,选择build并选中二进制构建视图中的文件夹,以便在导出插件时包含该文件夹。
那么你很可能不会正确加载bundle/plugin资源,这需要与常规java应用程序不同。这里是一个代码示例,给它一个尝试,让我知道如果它的工作。
public static String loadResourceAsString(String pluginId,
String resourcePath) throws IOException {
Bundle bundle = Platform.getBundle(pluginId);
URL url = bundle.getResource(resourcePath);
BufferedReader in = new BufferedReader(new InputStreamReader(
url.openStream()));
StringBuilder builder = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null) {
builder.append(inputLine + System.getProperty("line.separator"));
}
in.close();
return builder.toString();
}