Eclipse中如何访问插件的成员文件和文件夹

How to access the member files and folders of a plug-in in Eclipse

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

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

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转换为普通的fileURL,可能需要将插件jar解压到临时位置以便这样做。

您可以使用

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

它获取包含当前 class 或

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

通过插件 ID 访问包。