我有 2 个插件 A 和 B。在A的MANIFEST.MF中,我在Require-Bundle部分中有插件B。但是当我尝试从A获取B的资源时,例如
ClassFromA.class.getClassLoader().getResource('resource_from_B')
我得到null。如果我将B's资源(文件夹)放到A's根目录下,一切都会像魅力一样工作。我错过了什么吗?
注意:我读过 Lars Vogel 的 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
附注我还在 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();
}
注意:使用 URI 的 3-arg 构造函数也非常重要,以便正确转义文件系统字符。
最佳答案
使用
FileLocator.toFileURL(fileURL)
当您想要将 URL 与 File
一起使用时,而不是 FileLocator.resolve(fileURL)
。
当插件打包到 jar 中时,这将导致 Eclipse 在临时位置创建一个解压版本,以便可以使用 File
访问该对象。
关于eclipse - 如何从另一个插件获取资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23825933/