I want to specify images of a deck of cards to my code (blackjack game). Now if I want to show the image on screen I need to write the full path of the image from my computer, but if I create a JAR, it doesn't hold the images. How can I make them be part of the program?
Asked
Active
Viewed 25 times
1 Answers
0
Simply add images to your src folder - for example create package com.stackoverflow.images
and store files into.
Remember about set your path correctly (for example src/com/stackoverflow/images/yourImage.png
)

rzysia
- 737
- 1
- 11
- 26
-
I tried to store them at the default package (because that's where my code is), but it didn't work... You think that I need to name my package? – Almog Talker Mar 18 '15 at 07:06
-
genrally it's good idea to create package - try and we will see ;) – rzysia Mar 18 '15 at 07:11
-
OK, I tried but it doesn't work :( I tried to put the image inside the package and again inside the src folder. Nothing worked... – Almog Talker Mar 18 '15 at 07:15
-
Open your jar with winrar and tell what is inside – rzysia Mar 18 '15 at 07:17
-
look inside jar - it might be that picture is there, but your paths aren't correct – rzysia Mar 18 '15 at 07:20
-
I did not create a jar file just yet because I want to test that inside eclipse first. Do you need me to write the path? – Almog Talker Mar 18 '15 at 07:22
-
playerPanel.add(new JLabel(new ImageIcon("01.png"))); – Almog Talker Mar 18 '15 at 07:23
-
so the problem is that your program doesn't draw your picture? – rzysia Mar 18 '15 at 07:24
-
when you store it in package, you can set path to (for example) `src/yourPackage/01.png` – rzysia Mar 18 '15 at 07:26
-
Yeah!!!! It worked! The problem was that I didn't put src/ inside the path :) Thank you! – Almog Talker Mar 18 '15 at 07:30
-
I don't have enough reputation :( – Almog Talker Mar 18 '15 at 07:44
-
Actually there is still a problem. It works fine on eclipse but when I create a JAR file and run it, I can't see the images. So I opened the JAR file and inside there are 3 folders, one named blackjack (I think it the package one - it contains the name of the classes + end of .class), the second is the folder of the cards (the images), and the third is a folder named META-INF and inside there is a file named MANIFEST.MF. – Almog Talker Mar 18 '15 at 07:59
-
so maybe try to remove `src` from paths and create jar again? – rzysia Mar 18 '15 at 08:01
-
I'm sorry I don't know how to do it, I can't delete that folder or to move the game+cards outside of that folder... – Almog Talker Mar 18 '15 at 08:07
-
I mean to remove string `src` from path in java code ;) – rzysia Mar 18 '15 at 08:09
-
OH! LOL! Yeah I tried it already before I comment :) It didn't work. also inside eclipse... – Almog Talker Mar 18 '15 at 08:13
-
maybe this one will help :) http://stackoverflow.com/questions/12153590/load-java-image-inside-package-from-a-class-in-a-different-package – rzysia Mar 18 '15 at 08:20
-
There were several ideas, the ones I got is just putting the jar file with the cards folder in the same folder and it works. But I didn't understand the way to insert the images inside the resources of the project... – Almog Talker Mar 18 '15 at 08:30