如何从另一个插件获取资源?

Ily*_*iuk 8 eclipse eclipse-plugin eclipse-rcp

我有2个plguins .在A的MANIFEST.MF中,我在Require-Bundle部分中有插件B. 但是,当我试图让B的从资源一个

ClassFromA.class.getClassLoader().getResource('resource_from_B') 
Run Code Online (Sandbox Code Playgroud)

我变得空了.如果我将B的 resourсe(文件夹)放到A的根部,一切都像魅力一样.我错过了什么吗?

注意:我读过Lars Vogel的文章

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();
}
Run Code Online (Sandbox Code Playgroud)

^当我从eclipse运行我的插件时这个解决方案有效,但当我将它打包到jar并尝试从本地更新站点安装时我得到了

java.lang.IllegalArgumentException: URI is not hierarchical  
Run Code Online (Sandbox Code Playgroud)

PS我还在StackOverflow上阅读了几个相关问题,但未能找到答案:

解决方案:非常感谢@ greg-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();
} 
Run Code Online (Sandbox Code Playgroud)

注意:使用URI的3-arg构造函数以正确转义文件系统字符也非常重要.

gre*_*449 6

使用

FileLocator.toFileURL(fileURL)
Run Code Online (Sandbox Code Playgroud)

而不是FileLocator.resolve(fileURL)当你想使用URL时File.

当插件打包到jar中时,这将导致Eclipse在临时位置创建解压缩版本,以便可以使用该对象进行访问File.