3

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
  • 801
  • 2
  • 11
  • 27

6 Answers6

10

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

Sameer Technomark
  • 1,399
  • 1
  • 10
  • 12
  • 2
    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.) – Michael Plautz Nov 12 '13 at 15:15
  • 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. – wheresmycookie Jul 28 '15 at 16:19
6

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

indiv
  • 17,306
  • 6
  • 61
  • 82
danny
  • 61
  • 1
  • 2
  • 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 Nov 12 '13 at 15:04
2

you might need to load them as class path resources if they are within a jar. see: getClass().getClassLoader().getResourceAsStream(...)

Steven
  • 3,844
  • 3
  • 32
  • 53
  • 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? – Chiubaka Feb 03 '11 at 05:11
  • 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 – Steven Feb 03 '11 at 06:39
1

Use getResource() to load the images:

ImageIcon qmarkIcon = new ImageIcon(getClass().getResource("images/mark.gif"));
vinga
  • 1,912
  • 20
  • 33
0

If you're using JDK 1.7 or JDK 1.8, you might want to use the NIO.2 API.

for (FileSystemProvider provider : FileSystemProvider.installedProviders()) {
    if ("jar".equals(provider.getScheme()))
        return provider.newFileSystem((new File(Start.class
                .getProtectionDomain().getCodeSource().getLocation().toURI()))
                .toPath(), new HashMap<String, Object>());
}

If you enter this code into a method that returns a java.nio.file.FileSystem, you can call the method to get the FileSystem for the JAR file.

To get the path to access the files inside your JAR file, you can use the following method, which then allows you to read the files however you may want.

fileSystem.getPath("/images/image.gif")

If you would like to be able to run this in Eclipse, make sure you surround the call to the method with a try/catch IOException and assign to your FileSystem object the following.

new File(Start.class.getProtectionDomain().getCodeSource().getLocation().toURI())
        .toPath().toString();

This will allow you to run your program whether it's compressed into a JAR file or not.


I recommend you get used to using NIO.2, since it is a very powerful API.

ThePyroEagle
  • 153
  • 12
-1

If you add a folder to build path you can retrieve the images either in eclipse and when you exported it in jar file, just remember to don't reference the image with the path like img/myImage.gif but only myImage.gif !

Andrea Scarafoni
  • 937
  • 2
  • 13
  • 26