Java – How to read an image in Java when Toolkit.getDefaultToolkit() throws an AWTError

awtimagejava

I am reading image files in Java using

java.awt.Image img = Toolkit.getDefaultToolkit().createImage(filePath);

On some systems this doesn't work, it instead throws an AWTError complaining about sun/awt/motif/MToolkit.

How else can you create a java.awt.Image object from an image file?

Best Solution

I read images using ImageIO.

Image i = ImageIO.read(InputStream in);

The javadoc will offer more info as well.