我有一个Java插件。在此插件中,我有一个名为org.eclipse.epsilon.eol的程序包,在此程序中有一个名为configuration.png的图像。在此程序包的一个类中,我想使用下面的代码行。如何设置相对地址?

我尝试了几种方法,例如configuration.pngeol/configuration.png./eol/configuration.png,但是它们没有生效。我还在插件中创建了一个名为icon的文件夹,并编写了icon/configuration.png,但它没有生效。我该怎么办?

setDefaultPageImageDescriptor(ImageDescriptor.createFromFile(null, "configuration.png"));

最佳答案

您不能使用相对路径来访问插件(甚至是当前插件)中的资源。当插件打包为jar时,无法直接将内容作为文件访问。

要访问插件中的资源,必须使用FileLocator API:

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

URL url = FileLocator.find(bundle, new Path("path in the plugin"), null);

ImageDescriptor desc = ImageDescriptor.createFromURL(url);

10-02 05:10