0

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);

enter image description here

enter image description here

enter image description here

Jukeland
  • 19
  • 7
  • Which build manager do you use? Can you show us the project structure? Are all your images are located in ./src/main/resources directory? – Uchqun Tursunbayev Dec 24 '20 at 10:19
  • I use Ant. I have all the images in this path: ./src/resources, I don't use the main in between. Do I have to? The project structure is being displayed in the question above. – Jukeland Dec 24 '20 at 10:27
  • 1. How do you build the jar? 2. Show us how do you access the image file? Make sure you are doing it right: https://stackoverflow.com/a/45580/2000323 – Andrey Dec 24 '20 at 12:34
  • 1) I build the jar using this tutorial: https://www.youtube.com/watch?v=3Xo6zSBgdgk 2) I updated mhy question and now it is shown how I access the image file. – Jukeland Dec 24 '20 at 16:16
  • `I updated mhy question and now it is shown how I access the image file` - have you checked the link I referenced? Make sure you are correctly referencing the file resource. It seems you are not doing it right. – Andrey Dec 29 '20 at 16:43
  • No, I am doing it right, I just needed to move the jar file into the first directory which contains the src\resources directory. Nothing of what all of you helped. Thank you very much for your concern though. – Jukeland Dec 31 '20 at 14:57

2 Answers2

0

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

0

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

mark directory as resources root

szyjek
  • 581
  • 1
  • 4
  • 5
  • I right-click on the resources directory but the only thing that pops up is Excluded. There is no option for Resources Root. – Jukeland Dec 24 '20 at 11:40
  • Ok, so i think answer to your question is here: https://stackoverflow.com/questions/804150/ant-compile-doesnt-copy-the-resources – szyjek Dec 24 '20 at 11:52