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:
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.