Trying to load image using ImageIO.read(class.getResource(URL)) but getResource is returning null

寵の児 提交于 2019-11-28 13:55:24
haraldK

This problem is basically unrelated to ImageIO, but rather how Class/ClassLoader.getResource or getResourceAsStream works.

For an explanation, see this answer.

In any case, these ways of obtaining a resource will only be able to read from classpath (ie. user.dir will never help here).

This should work:

ImageIO.read(getClass().getResource("/path/to/resource"));

Where the path is relative to the root of the classpath (specified by the leading /).

If your resources are not on the classpath, simply use:

ImageIO.read(new File("path/to/resource");

Where the path is relative to the directory your application was launched from.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!