Yes, this is a duplicate of like 100 different posts, but none of the solutions have worked for me.
The way I am defining pictures in my game is this:
frontPage = ImageIO.read(new File("frontpage.png"));
It has worked fine so far, but now, I am exporting it as a jar file and converting it into an exe using Jar2Exe. I have never had a problem with this.
However, to export it and make it readable, I change it to:
frontPage = ImageIO.read(this.getClass().getResource("/frontpage.png"));
I am now getting this error:
java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at com.teamxf.Game.<init>(Game.java:80)
at com.teamxf.Game.main(Game.java:62)
Line 80 is the line above, where I define frontPage.
Line 62 is where I call the constructor in the main method. I define the images in the constructor.
What is causing this issue?
Edit: I am having the same error in eclipse when running it.
new File()
is completely different fromgetResource()
. The former loads the file from the file system and the latter loads it from the classpath. Both methods may yield same results while running your application from an IDE but running it from a packaged jar requires the resources to be on the classpath, which normally means they have to be inside the jar. If you use Maven as build tool, you just have to place your resources insrc/main/resources
to have them added to your jar.