0

OK guys, so I'm trying to compile my game into a jar file, but I can't get the loading of images to work. When run from NetBeans, all is fine. But in the JAR, the URL is always null.

Here is the code I'm using:

    URL url = this.getClass().getResource("/textures/Lava.jpg");

    BufferedImage sourceImage = null;

    try
    {
        sourceImage = ImageIO.read(url);
    }
    catch(IOException e)
    {
        System.out.println(e.getMessage());
    }

I have tried unziping the JAR and checking the contents, my textures folder is there and the images inside also. Any ideas what I'm doing wrong?

nbanic
  • 1,270
  • 1
  • 8
  • 11
Anderiel
  • 173
  • 2
  • 13

2 Answers2

1

you should get the URL path as relative path of your system.

Sanjay Rabari
  • 2,091
  • 1
  • 17
  • 32
1

Previously answered here:

Accessing a file inside a .jar file

Better use

new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/resources/" + filename)))
Community
  • 1
  • 1
noisy cat
  • 2,865
  • 5
  • 33
  • 51
  • -1 Don't repeat an answer from another question. If it's a duplicate, **don't answer** and flag it. – Duncan Jones May 13 '14 at 08:38
  • @Duncan I also flagged the post and I know it will be removed soon but we should let author know that it's answered already. – noisy cat May 13 '14 at 08:41
  • If you flag, a comment appears and that's sufficient notification to the author. If you post an answer, it may attract down-votes because it appears like you are just harvesting reputation. – Duncan Jones May 13 '14 at 08:42
  • Okay. Sorry then, didn't know that :) Next time I'll just flag. – noisy cat May 13 '14 at 08:43