Java: Unable to read image file

I am trying to load an png using this code:

BufferedImage image = ImageIO.read(new File(getClass().getResource(fileName).toString());

The file exists, in the directory exists, I have used this numerous times to load files. I am using eclipse and the classpath is configured, but for some reason I get this exception:

javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(Unknown Source)

This same code works in another project... I really don't know whats going on...

Jon Skeet
people
quotationmark

I would avoid trying to read using a File at all - just use the URL returned by getResource:

BufferedImage image = ImageIO.read(getClass().getResource(fileName));

Aside from anything else, that will still work when the resource is in a jar file, whereas it won't if you try to use File.

people

See more on this question at Stackoverflow