6

I have been browsing Stackoverflow all day looking for how to do this and I have not been successful yet

I am packaging a quick game I made into a executable jar but I didnt reference the images correctly I just referenced the files

background = ImageIO.read(new File("wood.jpeg"));

I have my classes in src default package Im not sure where I should add the images or if I have to add it to the build path or correct way of adding the images to the build path in the newest version of eclipse

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Mintybacon
  • 331
  • 2
  • 4
  • 11

2 Answers2

14

Files in a Jar are not files in the sense of a file on disk. They are simply a (possibly) compressed stream of bytes.

Java makes it easy to extract these "resources" from Jar files through the use of the ClassLoader

background = ImageIO.read(getClass().getResource("/wood.jpeg"));

Should work...

This will return a URL which ImageIO can use to load the resource.

You could also have a read of

And I could list some more. So, yeah, it gets asked a lot ;)

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 2
    +1 Very simple explanation, but you should also mention where the images should go in a project. e.g. src/main/resources for maven to pick up. – asgs Sep 19 '12 at 05:08
  • 2
    See also the [embedded-resource info. page](http://stackoverflow.com/tags/embedded-resource/info). – Andrew Thompson Sep 19 '12 at 05:08
  • @asgs I feel that point goes beyond the question. With the right build file/circumstances, we can *generate* images at build-time. – Andrew Thompson Sep 19 '12 at 05:15
  • @AndrewThompson Right, just wanted to let the OP know that there's a commonly used directory dedicated for resources. @-MadProgrammer Yes, that's a quick example. – asgs Sep 19 '12 at 05:20
  • I got this to work but only after dragging the image files into the bin folder although I have not been able to launch the execuatble jar since.. Thank you! – Mintybacon Sep 19 '12 at 19:20
  • correction I added the files to the bin for proof on concept to see if it would work after that I added the images to a source folder so they are added automatically to the bin and the jar it works fine in windows but when I run it on linux I get my program with no images again – Mintybacon Sep 19 '12 at 22:19
  • @Mintybacon try printing the results of `getClass().getResource("/wood.jpeg")` to the console (using `System.out.println`) to verify that the image was found within the Jar resource. Technically, if it worked on one platform, this approach should work on others – MadProgrammer Sep 19 '12 at 23:03
0

Try using Constructing Runnable Jar using the Eclipse.

R_Click on the Project in the Package Explorer ---> 
Export ---> Runnable JAR file ---> 
Select the option of Package required libraries into generated JAR
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • 3
    This option refers to libraries rather than resources, and it does not address the problem of attempting to load the zip entry as if it were a `File`. – Andrew Thompson Sep 19 '12 at 05:11