[英]java.lang.IllegalArgumentException: input == null! at javax.imageio.ImageIO.read(Unknown Source)
I am trying to set the icon image like 我正在尝试将图标图像设置为
private void formWindowActivated(java.awt.event.WindowEvent evt) {
try {
Image img=ImageIO.read(getClass().getResource("images/logo.png"));
setIconImage(img);
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
The code seems to work perfectly after compilation on NetBeans but I get the IllegalArgumentException if I run the jar file from cmd.I don't seem to understand how the image can be visible in first case and not on the other one. 在NetBeans上编译后,代码似乎可以正常工作,但是如果我从cmd运行jar文件,则会收到IllegalArgumentException。我似乎不明白如何在第一种情况下而不是另一种情况下可见图像。
This will happen under Windows (case insensitive file system) where loading may succeed for say Images/Logo.PNG
on the unpacked class path, and will fail when starting from the jar (zip Archive), where paths are case-sensitive. 这将在Windows(不区分大小写的文件系统)下发生,在Windows上,未压缩的类路径上的
Images/Logo.PNG
可能会成功加载,而从jar (zip存档)启动(路径是区分大小写的)时,加载会失败。
Correcting the case of a file may be cumbersome (version control and such), hence a rename might be more useful. 更正文件大小写可能很麻烦(版本控制等),因此重命名可能更有用。
Remarks: 备注:
If the (actual!) class is in package xyz
, the directory path in the jar is x/y/z/images/logo.png
. 如果(actual!)类位于包
xyz
,则jar中的目录路径为x/y/z/images/logo.png
。
Hence alternatives to consider are non-child class reference and absolute path: 因此,要考虑的替代方法是非子类引用和绝对路径:
Image img=ImageIO.read(Xyz.class.getResource("images/logo.png"));
Image img=ImageIO.read(getClass().getResource("/u/v/w/images/logo.png"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.