我有一个Java插件。在此插件中,我有一个名为org.eclipse.epsilon.eol
的程序包,在此程序中有一个名为configuration.png
的图像。在此程序包的一个类中,我想使用下面的代码行。如何设置相对地址?
我尝试了几种方法,例如configuration.png
,eol/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);