Question

I'm coding a Java application which it needs to load some images from a resource folder. I've added that folder to the Java Libraries. I make it read the BufferedImages using this code

public BufferedImage grabImage(String fileName, int subImgNumber){
    ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    BufferedImage tmp;
    if(!fileName.equals("null"))
    try {
        tmp = ImageIO.read(classloader.getResourceAsStream("../res/textures/"+fileName+".png"));
        maxIndex = tmp.getWidth()/32 - 1;
        if(subImgNumber > maxIndex){
            subImgNumber = maxIndex;
        }else if(subImgNumber < 0){
            subImgNumber = 0;
        }
        requestImage = tmp.getSubimage(subImgNumber * 32, 0, 32, 32);
    } catch (IOException e) {
        e.printStackTrace();
    }
    else
        requestImage = null;
    return requestImage;
}

If I test the application in Eclipse it works fine and pretty fast, but when i export the jar file it doesn't work and throw me this exeption

Exeption

I've checked inside the .jar file and it has all the images i need. Is something wrong with my code?

Was it helpful?

Solution

Fixed, I've changed the method getResourceAsStream() with getResource() it fix all the problems

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top