Question

I'm programming a GUI in Java (using Swing of course) and while running my application in Eclipse everything is fine and with icons and whatnot, but after exporting my application the icons are gone.

The icons are located under the class-path in the following directory resources/icons.

JButton browseButton = new JButton("Browse", new ImageIcon("resources/icons/browse.png"));
Was it helpful?

Solution

Seems like as guided by @kleopatra, this surely is the right way. When you create resources folder, make sure you do it by right clicking your project, then go to source folder and name it as resources/icons and then manually put your images in that folder, and go back to your eclipse and refresh your project. Now you can use these images in your runnable jar also.

Now in order to use your image/icon in your Classes use this

private ImageIcon getImage(String path)
{
    URL url = getClass().getResource(path);
    System.out.println(url);
    if (url != null)
        return (new ImageIcon(url));
    return null;
}

Here your path will be the name of the image prefix it by a forward slash, which is inside your resources/icons folder like "/myIcon.png" I wanted to be sure, before i post my answer, so tested it. Else give me some time, I will put all steps somewhere for you to see.

More info related to the whole thingy can be found on this another answer of mine :

Load ImageIcon Exception

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top