如何使用java从包中加载图像图标

如何使用java从包中加载图像图标,java,swing,package,embedded-resource,imageicon,Java,Swing,Package,Embedded Resource,Imageicon,我要用密码 URL iconURL = getClass().getResource("/some/package/favicon.png"); // iconURL is null when not found ImageIcon icon = new ImageIcon(iconURL); frame.setIconImage(icon.getImage()); 用于从软件包加载图像并设置为myJFrame的图标 图像位于packagecom.app.image中,名称为logo.png

我要用密码

URL iconURL = getClass().getResource("/some/package/favicon.png");
// iconURL is null when not found
ImageIcon icon = new ImageIcon(iconURL);
frame.setIconImage(icon.getImage());
用于从软件包加载图像并设置为my
JFrame
的图标

图像位于package
com.app.image
中,名称为
logo.png

当我构建URL时:

URL iconURL = getClass().getResource("/com/app/image/logo.png");

我在
iconURL
中得到
null
。哪里有问题?

类路径文件夹的根目录下是否有
com
文件夹?是
com
是根包。映像实际上包含在Jar文件或类路径中吗?如果您有一个Jar文件,请将其解压缩并检查其内容,并确保图像存在(您认为应该存在的位置)@MadProgrammer我检查Jar存档,但图像不包括在Jar中。@triotro好吧,这是您的问题,您无法加载不存在的资源。如何修复它需要更多的上下文,尤其是如何创建Jar