【问题标题】:How to access the member files and folders of a plug-in in Eclipse如何在Eclipse中访问插件的成员文件和文件夹
【发布时间】:2016-10-17 22:07:53
【问题描述】:

我们有一个导出到 RCP 产品的插件。在插件中,有一个文件夹,里面有一些文件。如何以编程方式访问Eclipse中某个文件夹下的插件文件?

【问题讨论】:

    标签: eclipse eclipse-plugin eclipse-rcp


    【解决方案1】:

    使用org.eclipse.core.runtime.FileLocator 类访问插件中的文件。

    Bundle bundle = ... bundle containing the files
    
    IPath path = new Path("relative path in plug-in of the file");
    
    URL url = FileLocator.find(bundle, path, null);
    
    URL fileUrl = FileLocator.toFileURL(url);
    

    FileLocator.find 方法返回的url 使用 Eclipse 特定的方案,并且只能与某些 Eclipse API 一起使用。

    FileLocator.toFileURL 调用将 URL 转换为普通的 file URL,可能需要将插件 jar 解压到临时位置才能执行此操作。

    您可以使用

    获取Bundle
    Bundle bundle = FrameworkUtil.getBundle(getClass());
    

    获取包含当前类的包或

    Bundle bundle = Platform.getBundle("plugin id");
    

    通过插件 ID 访问包。

    【讨论】:

    • platform:/plugin 这个解决方案怎么样 FileLocator 导致在文件系统文件夹中找到文件而不是在插件中。
    • 人成功了!我使用了新文件 (fileUrl.toURI()) 然后它变成了文件。
    • 如何从中生成文件。 fileUrl.toURI 由于 eclipse 错误 #145096 失败,当文件夹名称中有空格时失败。
    • this question 对此进行了一些讨论。我自己没有遇到过这个问题。
    • 我使用了 URIUtil.toURI(fileUrl) 。
    猜你喜欢
    • 2012-04-04
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    相关资源
    最近更新 更多