2

I have a snake game written in java but when I export the project as Runnable Jar, the images do not load. They work fine in eclipse. What is the problem?

My loadImages method looks as following:

private void loadImages() { 
    ImageIcon snakeBlockIcon = new ImageIcon("snake/images/snakeBody20px.png");
    snakeBlockImage = snakeBlockIcon.getImage();
    snakeBlockImage = snakeBlockImage.getScaledInstance(DOT_SIZE, DOT_SIZE, Image.SCALE_DEFAULT);
...

My project is structured as following:

(SourceFolder)Projects > (source package) snake > (package) images

(SourceFolder)Projects > (source package) snake > (package) snake > .java files

Thank you!

EDIT: earned enough rep to post the image

path

ucMedia
  • 4,105
  • 4
  • 38
  • 46
Dimebag
  • 833
  • 2
  • 9
  • 29
  • Are the images in the runnable jar? What path names do the images have within the jar? – Kenster Jan 16 '15 at 16:21
  • After some tries, opened the .jar but the images are not inside. – Dimebag Jan 16 '15 at 16:49
  • possible duplicate of [Java in Eclipse: Where do I put files on the filesystem that I want to load using getResource? (e.g. images for an ImageIcon)](http://stackoverflow.com/questions/270197/java-in-eclipse-where-do-i-put-files-on-the-filesystem-that-i-want-to-load-usin) – Barett Jun 24 '15 at 17:09

2 Answers2

1

In a jar, you need to access files as a resource by using code like this (similar for .txt .WAV):

ImageIcon ic = new ImageIcon(getClass().getResource("/machetelife.jpg"));

I don't know if eclipse packages it differently.. I usually use the cmd to package jar files.

Woodrow
  • 136
  • 2
  • 10
  • For the sake of information; your jar file in the same directory as machetelife.jpg should be able to access it normally without accessing it as a resource; though it would defeat the purpose of a jar somewhat. – Woodrow Jan 16 '15 at 17:31
  • This works, finally... but just when exporting. In eclipse, I get NullPointerException. Any way I can make it work in both places with the same code? – Dimebag Jan 16 '15 at 17:51
  • Ya, I would create a File object of "/machetelife.jpg", File file = new File("machetelife.jpg"); And if(file.exists()) { //access jpg via file object } else { //access jpg as resource } – Woodrow Jan 16 '15 at 18:41
0

Eclipse won't include in the jar anything else than code, so you'll have to either add the neded files manually or modify the way Eclipse generates the jar.

This second option can easily be done by first exporting the ant build file from the "Generate runnable jar" screen and then editing it to include your files.

To modify it find the jar task, it should be the first element inside the target element with a name attribute of "build_run_jar" (<target name="build_run_jar">), and inside it add the following element:

<fileset dir="path/to/your/files"/>

Substituting the path with the relative or absolute path to the folder containing your files.

If you want to include specific files from the directory you can explicitly include them (note that this way ONLY the files you specify will be included):

<fileset dir="your/path"/>
    <include name="filename"/>
</fileset>

For more informations you can read the Ant manual about the Jar task

Iamsomeone
  • 233
  • 2
  • 15