且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java IDE - Eclipse,导入资源

更新时间:2023-12-04 11:56:28

您可能在eclipse项目中创建了图标目录作为单独的源文件夹(它具有由文件夹图标和包图标组成的图标)。源文件夹只是开发过程中的逻辑元素(因此您可以逻辑地分离应用程序的模块),它们不是导出的应用程序的一部分。



因此,简单的解决方法可能是在其他源文件夹中创建一个图标通常称为src)。导出后,这些包将作为子文件夹存在。


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.

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.