我有2个pligins A和B。在A的MANIFEST.MF中,我的插件B在Require-Bundle部分。但是,当我尝试从一个得到B的资源像如何从另一个插件获取资源?
ClassFromA.class.getClassLoader().getResource('resource_from_B')
我得到空。如果我把B的资源(文件夹)到A的根一切都像一个魅力。我错过了什么吗?
注:我读的Lars沃格尔article
Bundle bundle = Platform.getBundle("de.vogella.example.readfile");
URL fileURL = bundle.getEntry("files/test.txt");
File file = null;
try {
file = new File(FileLocator.resolve(fileURL).toURI());
} catch (URISyntaxException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
^当我从Eclipse中运行我的插件,但是当我收拾它JAR和尝试从本地更新站点,我得到安装此解决方案的工作
java.lang.IllegalArgumentException: URI is not hierarchical
PS我也看到了在StackOverflow的几个相关问题,但没能找到答案:
SOLUTION: 非常感谢@格雷格 - 449。所以,正确的代码是:
Bundle bundle = Platform.getBundle("resource_from_some_plugin");
URL fileURL = bundle.getEntry("files/test.txt");
File file = null;
try {
URL resolvedFileURL = FileLocator.toFileURL(fileURL);
// We need to use the 3-arg constructor of URI in order to properly escape file system chars
URI resolvedURI = new URI(resolvedFileURL.getProtocol(), resolvedFileURL.getPath(), null);
File file = new File(resolvedURI);
} catch (URISyntaxException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
注:也很非常重要使用URI的3参数的构造函数,以便正确地逃避文件系统的字符。
哦,我的!你真棒:-) –
@Lars Vogel我想你在你的文章中有错误 - http://blog.vogella.com/2010/07/06/reading-resources-from-plugin/ –