0

Hopefully someone can help me with something that is probably very simple. I'm new to Java coding (2 weeks in), and using Eclipse IDE under Linux. I'm currently in the process of creating a JFrame application, everything so far is going well.

I have one little snag though - I have included a set of Icons and assigned them to a JLabel, and have them displayed. Upon exporting an Executable JAR, they are not in the JAR as a resource. If I open the JAR file, I can see the Images, in the ROOT of the JAR, not organized in their respective folders. (icons/, etc).

TL;DR - How do I import resources, in their folders, into a JAR as a resource.

    public void drawCategoryIcons() {
    for (int i = 0; i < aspCategories.length; i++) {
        Icon pcIcon = new ImageIcon(getClass().getResource( "/icons/" + cats[i]));

        aspCategories[i] = new JLabel("", JLabel.CENTER);
        aspCategories[i].setIcon(pcIcon);
        panel.add(aspCategories[i], "w 200, center");
    }
}

If I RUN the project within Eclipse, everything works as it should. Exporting it, I get tons of errors.

Eric J.
  • 147,927
  • 63
  • 340
  • 553

1 Answers1

0

You have probably created the icons directory as separate source folder in the eclipse project (it has an icon composed of a folder icon and a package icon then). Source folders are just logical elements during development (so you can separate modules of your application logically), they are not part of the exported application. Only everything inside source folders is exported.

Therefore a simple workaround might be to instead create a package "icons" in your other source folder (typically called "src"). Those packages will exist as sub folders after export.

Bananeweizen
  • 21,797
  • 8
  • 68
  • 88