![](/img/trans.png)
[英]ImageIO.read(getClass().getResource(imgStr)) returns null in JAR
[英]Java ImageIO.read(getClass().getResource()) returns null
线
andImg = ImageIO.read(getClass().getResource("gate_and.png"));
失败
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null!
我正在使用 Eclipse,在 bin 文件夹下的导航视图中有文件 gate_and.png,表明该文件在构建路径中。
在包资源管理器视图中,我有
project/src/view/class - This is the class that has the code above.
和
project/images/gate_and.png
我右键单击项目文件夹 > 构建路径 > 链接源以将图像文件夹添加为源,再次执行此操作会提供一个确认消息,表明图像已在源中。
我也尝试将gate_and.png 更改为images/gate_and.png 和/images/gate_and.png,但由于图像gate_and.png 在bin 文件夹中,我认为原始是正确的。
假设你的班级在包view.random.name
,那么
getClass().getResource("gate_and.png")
将在
/view/random/name/gate_and.png
相对于类路径的根。 那里显然没有该名称的资源。
通过将project/images
设置为构建路径条目,Eclipse 将在类路径中包含其中的所有内容。 因此,您的资源将出现在
/gate_and.png
你可以访问它
getClass().getResource("/gate_and.png")
注意前导/
意味着开始查看类路径的根,即。 这是一个绝对路径。
所有这些规则都在javadoc 中进行了解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.