Exporting Images with JAR in Eclipse (Java)

22,949

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

Eclipse screenshot showing were to put images

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"));
Share:
22,949
Chiubaka
Author by

Chiubaka

Updated on November 21, 2020

Comments

  • Chiubaka
    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
    Chiubaka about 13 years
    Ok, 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
    Steven about 13 years
    place 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
    Michael Plautz over 10 years
    Thanks @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 the img directory appeared at the base/root level of my jar file. To get around this, I created a folder called lnk, and then created a symbolic link to my img folder (in Windows, this can be done with JUNCTION) within the lnk folder. Now, when I added lnk "as source folder", the images were in the img folder in the jar.
  • Michael Plautz
    Michael Plautz over 10 years
    I think this breaks convention just a bit. It is helpful for only .java source files (and other actual source code files) to be in the src 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
    wheresmycookie over 8 years
    I signed in specifically to upvote @MichaelPlautz's comment. Please don't add images in with your .java files just because it happens to work.