简体   繁体   中英

How to include the resources folder in a jar file

I have finished a project and I have been testing it by running it through IntelliJ Idea and it was working fine. I created a.jar executable file but when I run it the images I used for the GUI are not being displayed. I did some searching about that problem but I didn't quite understand how I am supposed to include the resources folder (where the images are stored) into the.jar file. I added two images of how the GUI should be displayed and how the.jar file displays it. Can I get some help please?

Thank you in advance!

I access the images like that:

JLabel lbl = new JLabel("", new ImageIcon("src\\resources\\background.png"), JLabel.CENTER);

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

Follow these steps:

  1. click project -> properties -> Build Path -> Source -> Add Folder and select resources folder.

  2. create your JAR!

EDIT: you can make sure your JAR contains folder by inspecting it using 7zip.

Reefer this link as well How do I add a resources folder to my Java project in Eclipse

Since you are using InteliJ please right click on "resources" directory -> mark directory as -> resources root. Then create jar again.

将目录标记为资源根

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM