0

I am trying to export an image inside my runnable jar. After some research i have come across this method:

ImageIO.read(getClass().getClassLoader().getResource("image path"));

After following several response and looking at the API, i have attempted this myself. My current directory set up is as follows:

enter image description here

This is my code:

cherryImg = ImageIO.read(getClass().getClassLoader().getResource("/res/cherry.png"));

which is being called from the Snake.java source file.

This is the error message:

Exception in thread "main" java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(Unknown Source)
    at cje.chris.edwards.snake.game.Snake.<init>(Snake.java:51)
    at cje.chris.edwards.snake.game.Snake.main(Snake.java:156)

Snake.java:51 is the line of code posted above. I understand this is because it cannot find the resource, what am i doing wrong? I have added the res folder to my build path.

I have also tried:

cherryImg = ImageIO.read(getClass().getClassLoader().getResource("res/cherry.png"));

This produces the same error message.

Answer:

cherryImg = ImageIO.read(getClass().getClassLoader().getResource("cherry.png"));

Thanks.

chris edwards
  • 1,332
  • 4
  • 13
  • 31

1 Answers1

1

I think your problem is the leading "/"

"/res/cherry.png"

Will make it an absolute path (from your root of your drive) not a relative path from your starting from the root of your jar

Making it "res/cherry.png" should work

dkatzel
  • 31,188
  • 3
  • 63
  • 67