Getting images from a .jar file

14,305

Solution 1

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.

Solution 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.
Share:
14,305
DCSoft
Author by

DCSoft

Java programmer, I make java games.

Updated on June 30, 2022

Comments

  • DCSoft
    DCSoft almost 2 years

    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
      Andrew Thompson almost 12 years
      What is the value of fileName? What is the structure of the Jar? From what package is the class that contains those methods?
    • DCSoft
      DCSoft almost 12 years
      Image player1 = loadImage("res/player1.png"); the bit in quotes is the value of fileName
  • DCSoft
    DCSoft almost 12 years
    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?
  • Mattias Isegran Bergander
    Mattias Isegran Bergander almost 12 years
    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.
  • DCSoft
    DCSoft almost 12 years
    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.
  • Mattias Isegran Bergander
    Mattias Isegran Bergander almost 12 years
    Try this: Right click the res folder, build-path -> use as source folder. Try and export again.
  • DCSoft
    DCSoft almost 12 years
    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");
  • Mattias Isegran Bergander
    Mattias Isegran Bergander almost 12 years
    Move them into your normal src tree perhaps then? Easier. Can have them in a special sub folder/package there if you want that.
  • DCSoft
    DCSoft almost 12 years
    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.
  • Mattias Isegran Bergander
    Mattias Isegran Bergander almost 12 years
    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 /
  • DCSoft
    DCSoft almost 12 years
    the documents directory where I created the jar. I have the res folder in there and the program works.
  • Mattias Isegran Bergander
    Mattias Isegran Bergander almost 12 years
    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.
  • kentcdodds
    kentcdodds almost 12 years
    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 :)
  • nIcE cOw
    nIcE cOw almost 12 years
    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, this might can help you or else do follow the steps mentioned here
  • DCSoft
    DCSoft almost 12 years
    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.