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

标签 eclipse eclipse-rcp eclipse-plugin

我有 2 个插件 AB。在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/

相关文章:

java - 是否可以使用 RCP Updatesite 将 Eclipse RCP 应用程序更新为新的 Eclipse

java - 如何检测eclipse插件未启动的原因?

java - 当我从 Eclipse (Juno) 运行 Sonar 本地分析时出错 - 我使用 maven 来构建

java - 启动tomcat7 :run maven plugin within eclipse and debug

java - Eclipse RCP 插件中的多个 slf4j 绑定(bind)

java - 如何正确结束 Eclipse 启动配置执行?

java - Eclipse 平台中的 Canvas 是什么

java - Eclipse 中运行的 Tomcat 实例的 "work"目录在哪里?

java - 如何将 Datanucleus 与多个 Eclipse 项目一起使用?

Eclipse RCP 的 Maven 原型(prototype)