3

I need to get res folder to compile when I export a executable jar file in eclipse also when I use the getClass().getResource() method it doesn't work.

Current reading image code

public Image loadImage(String fileName) {
    return new ImageIcon(fileName).getImage();
}

Code that doesn't work

public Image loadImage(String fileName) {
    return new ImageIcon(getClass().getResource(fileName).getImage();
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
DCSoft
  • 217
  • 1
  • 4
  • 17

2 Answers2

3

I have now fixed the problem - this is the code that works

 public BufferedImage loadImage(String fileName){

    BufferedImage buff = null;
    try {
        buff = ImageIO.read(getClass().getResourceAsStream(fileName));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
    return buff;

}

The value of fileName is just an image name eg BufferedImage img = loadImage("background.png");

Thank you all for your help.

DCSoft
  • 217
  • 1
  • 4
  • 17
  • No still I guess you doing it wrong, while using Eclipse, had you tried visiting this post : [HOW TO ADD IMAGES TO YOUR ECLIPSE PROJECT](http://stackoverflow.com/questions/9864267/load-icon-image-exception/9866659#9866659), this might can help you or else do follow the steps mentioned [here](http://gagandeepbali.uk.to/gaganisonline/webpages/makejareclipse.html) – nIcE cOw May 12 '12 at 19:09
  • Actually this code works, I had to create a folder called res and right click on it in Eclipse and click Build Bath: Use As Source Folder and then the code works and yes I had looked at those posts before. – DCSoft May 12 '12 at 20:14
2

Either:

  • your path is wrong, you should get an error message if so, check it to see what path it actually tries, might not be what you think. Or debug and see what path it tries or even just print the path it tries.

or

  • it's not in the jar. Check the jar file with a zip-program and/or rename it to have zip in the and open it to check that the file is really there.
Mattias Isegran Bergander
  • 11,811
  • 2
  • 41
  • 49
  • okay thanks also i can't seem to get eclipse to compile the res folder in the jar, do I need to add it to the classpath? – DCSoft May 07 '12 at 22:07
  • You are using the export function from the file menu I guess? In the wizard you get there you will have to include the res folder and/or the files and/or file types. – Mattias Isegran Bergander May 07 '12 at 22:09
  • yeah that's what I'm doing but when I export a runnable jar file I don't have that option that is only available with the export jar file. – DCSoft May 07 '12 at 22:14
  • Try this: Right click the res folder, build-path -> use as source folder. Try and export again. – Mattias Isegran Bergander May 07 '12 at 22:21
  • Okay that worked, but the images that were in res folder now they are in the main directory of the jar file so the code for reading the file is wrong code - Image player1 = loadImage("res/player1.png"); – DCSoft May 07 '12 at 22:30
  • Move them into your normal src tree perhaps then? Easier. Can have them in a special sub folder/package there if you want that. – Mattias Isegran Bergander May 07 '12 at 22:33
  • I have changed the Image player1 = loadImage("res/player1.png"); to Image player1 = loadImage("player1.png"); because they are in the same directory but that still did not work. I also noticed that if I use Image player1 = loadImage("res/player1.png"); and I compile it to a runnable jar and I have the res folder alongside the jar in the same directory it works. – DCSoft May 07 '12 at 22:45
  • Same directory as what? As the class file? In the same package as well? The path is the directory structure within the jar file (OR outside it) and is either: 1) relative to your class file which you call `getClass()...` on or 2) absolute path in the jar file if you start the path with `/` – Mattias Isegran Bergander May 07 '12 at 22:55
  • the documents directory where I created the jar. I have the res folder in there and the program works. – DCSoft May 07 '12 at 22:57
  • Ah ok yes it will pick up it from there too . So you path is wrong (or files not there again), my previous comment tells you how the path is located. Double check the jar file where it is. Inside the jar file as res/player1.png or player1.png or in the same directory as the class file perhaps? Change your path (or file location) accordingly. – Mattias Isegran Bergander May 07 '12 at 23:07
  • I'm pretty sure when you're dealing with resources, you have to start the file path with `/` so yours would be `/res/player1.png/`. Try that :) – kentcdodds May 12 '12 at 13:18