urgent guidance required to access image using class navigation

hello , in the program that i am developping, i have to access a list of files within the package of the project. it runs well when i run the program within eclipse but when i export it to a runnable jar, it seems that the path for my image folder changes.
From research i ended with solutions like using Class.getResource
or get ResourceStream
which i have never used before and it seems that i am not being able to implement it.

plz its urgent help my image folder and Class(which call the images) is as follows.
followed by my code extract.

http://s25.postimg.org/fv4mkxazz/location.png

http://s25.postimg.org/df2t72sxb/code_extract.png

plzz help me out, i got to submit an executable jar in less than 5 hours

Can’t use File() when getting stuff from inside a jar, as the jar is actually a zip, and the “files” inside aren’t actually files.
You have to work with InputStreams:


buffImg = ImageIO.read(ThumbnailsList.class.getClassLoader().getResourceAsStream("jp/images/thing.png"));

Don’t use Files, if your resource is not in a File.

okay how to access all the images in the folder(or package) then… i dont really knw how to access ALL the images from the folder.
my next instruction is to loop over all the images and add them in an array.

either hardcode the filenames, or create a pattern to your file names:


jp/images/sprite-1.png
jp/images/sprite-2.png
jp/images/sprite-3.png
jp/images/sprite-4.png
jp/images/sprite-5.png


for(int i=0; i<5; i++) {
   String path = "jp/images/sprite-"+(i+1)+".png";
   buffImgs[i] = ImageIO.read(ThumbnailsList.class.getClassLoader().getResourceAsStream(path));
}

okay thanks :slight_smile:
i take it that we cannot do it in a dynamically way… (without knowing the file names)

i think i’ll save the filenames in a text file and then read it through the stream. and recover the images.

You can do it, but you’ll have to iterate over the contents of a JAR/ZIP, with ZipInputSteam