Exporting Images with JAR in Eclipse (Java)
Solution 1
you might need to load them as class path resources if they are within a jar. see: getClass().getClassLoader().getResourceAsStream(...)
Solution 2
Something I would have found useful with this answer is the following: make sure you put your images/files in the same eclipse folder (or sub-folder below) as your source code. I created a folder "images_ignored" using eclipse, added it to the build path but still it refused to be included in my JAR file (when creating an executable JAR).
Solution 3
Just drag the images folder into your Eclipse project, then choose to "Copy New Folder" or "Copy File and Folder" depending on Eclipse version, and then right click on the image folder (in Eclipse) and --> build path "use as source folder".
Solution 4
Use getResource() to load the images:
ImageIcon qmarkIcon = new ImageIcon(getClass().getResource("images/mark.gif"));
Chiubaka
Updated on November 21, 2020Comments
-
Chiubaka over 3 years
I've been working on a little project that requires external images for display. I'm not all that familiar with how to use Eclipse and this is my first time attempting to export a completed project so I can share it with others. Right now, it seems the only way I can get my images to show up is if I assign a specific folder on my hard drive and have the image paths in the code go to that. I'm looking for a way to export the images as part of my JAR or as part of the same package so when I go to send this program to other people, I don't have to send them a separate archived folder of images. I'd also be interested in learning what I need to do to have my code reference the images within that package so they'll work without an external folder. I have read about some kind of package system within Eclipse, but have thus far had no luck in figuring out how to use it. Could use some explicating!
Thanks in advance to anyone willing to give me their two cents.
-
Chiubaka about 13 yearsOk, well, first problem then is that I apparently have no idea how to get them into the JAR using Eclipse. Can you maybe explain that to me?
-
Steven about 13 yearsplace them on the class path. so, find a eclipse source folder and put them in it. or whatever folder they are in - just right click and click add to build path
-
Michael Plautz over 10 yearsThanks @danny, this answer addresses what I was trying to do. However, when you do this, it removes the bottom-level folder. For example, I had a folder called
img
that stored all of my images, and when I did "use as source folder", all of the contents of theimg
directory appeared at the base/root level of myjar
file. To get around this, I created a folder calledlnk
, and then created a symbolic link to myimg
folder (in Windows, this can be done withJUNCTION
) within thelnk
folder. Now, when I addedlnk
"as source folder", the images were in theimg
folder in thejar
. -
Michael Plautz over 10 yearsI think this breaks convention just a bit. It is helpful for only
.java
source files (and other actual source code files) to be in thesrc
directory. I think @danny's answer is most sufficient, to right click on the desired folder and go to build path, "use as source folder". This way you can easily separate resources (images, sounds, source, config, etc.) -
wheresmycookie over 8 yearsI signed in specifically to upvote @MichaelPlautz's comment. Please don't add images in with your .java files just because it happens to work.