Thursday, May 10, 2007

Plugin your images

Let AbstractUIPlugin handle sharing images with your plugins. It has its own ImageRegistry that handles storing, disposing, and retrieving of images.

A simple override of initializeImageRegistry(ImageRegistry) and you are on your way to Image sharing bliss.

public class Activator extends AbstractUIPlugin {
    public static final String ID = "rcp.eclipse";
    public static final String MY_IMAGE_ID = 

    protected void initializeImageRegistry(ImageRegistry registry) {
        Bundle bundle = Platform.getBundle(ID);

        ImageDescriptor myImage = ImageDescriptor.createFromURL(
                               new Path("icons/myImage..gif"),
        registry.put(MY_IMAGE_ID, myImage);

Put your image into workspace/rcp.eclipse/icons/myImage.gif and then access it in your code.

AbstractUIPlugin plugin = Activator.getDefault();
ImageRegistry imageRegistry = plugin.getImageRegistry();
Image myImage = imageRegistry.get(Activator.MY_IMAGE_ID);


Fraer9 said...

Great thanks !

I was getting a org.eclipse.jface.resource.DeviceResourceException: Unable to create resource FileImageDescriptor when setting the image of an Action with

final String iconPath = FileLocator.toFileURL(
.getClass(), iconPath));

No more problems with :

ImageDescriptor myImage = ImageDescriptor.createFromURL(
new Path("icons/myicon.gif"),null));

Thanks a lot man :)

jodowd said...

If NOT using an Activator class and resulting imageRegistry...

ImageDescriptor.createFromURL(FileLocator.find(Platform.getBundle(plugin_id), new Path("icons/"),null);

Murthy said...


Thanks a lot for the post. It helped me resolve my issue of getting a image to be displayed.

I did not have a activator and have a plugin class instead. And i was earlier trying to get the descriptor as below : (but i never worked)

public static ImageDescriptor getImageDescriptor(String path) {
return AbstractUIPlugin.imageDescriptorFromPlugin("MyPlugin", path);

After changing it as below, it worked.

public static ImageDescriptor getImageDescriptor(String path) {

ImageDescriptor myImage = ImageDescriptor.createFromURL(
new Path("icons/file.gif"),null));

return myImage;


Thanks again !

kapitan said...

Hi guys!
I've tried your solution but it throws this bad error. It seems it cannot find the file, but it obviously is inside the jar and in that folder.

org.eclipse.core.runtime - org.eclipse.jface - 0 - /com/views/images/user.gif /com/views/images/user.gif
at org.eclipse.osgi.framework.internal.protocol.bundleentry.Handler.findBundleEntry(
at org.eclipse.osgi.framework.internal.core.BundleResourceHandler.openConnection(

What am I missing?

aneelak said...

Thank you so much! This was great help! I had been struggling to incorporate this feature for a couple of days!