导出后,java无法找到/绘制图像
你好 stackoverflowites,
我正在开发一个 2d 游戏,当我在 eclipse 中运行它时,我的程序加载了它的所有图像和资源。但是,我正在使用不一定推荐的方式来创建图像和图像图标,如下所示:
bannerLogo.setIcon(new ImageIcon("/res/client/BannerHeader.jpg"));
现在,当导出到 jar 时,它不会显示图像(这是预期的,因为我没有使用正确的方式。)
我搜索了正确的方法,我发现是:
URL imgURL = getClass().getResource("/res/client/BannerHeader.jpg");
Image bannerImg = Toolkit.getDefaultToolkit().getImage(imgURL);
bannerLogo.setIcon(new ImageIcon(bannerImg));
这也不起作用,我得到一个“未捕获的错误获取图像:”跟踪,它只是告诉我我的 URL (imgURL) 为空。 (这是当我在 Eclipse 中运行时,请注意,我什至还没有导出它)
我认为它与我的类路径有关,但是我不知道是什么。
在eclipse内部,我的包结构如下:(
父目录,项目名称)
+src文件夹,在构建路径+资源文件夹(“res”)上有普通包等
,而不是在构建路径上(在构建上尝试过)路径,没有任何改变)
+++资源文件夹的子目录
我不知道在这里做什么。抱歉,文字墙。
Hello stackoverflowites,
I am in the process of developing a 2d game, and when I run it inside of eclipse, my program loads all of it's images and resources fine. However, I am using the not-necessarily-recommended way of creating my images and image icons, which is shown below:
bannerLogo.setIcon(new ImageIcon("/res/client/BannerHeader.jpg"));
Now, when exporting to a jar, it does not show the image (which is expected since im not using the correct way.)
I searched for the correct way to do it, which I found was:
URL imgURL = getClass().getResource("/res/client/BannerHeader.jpg");
Image bannerImg = Toolkit.getDefaultToolkit().getImage(imgURL);
bannerLogo.setIcon(new ImageIcon(bannerImg));
And that didn't work either, I get an "Uncaught error fetching image: " trace, which just tells me that my URL (imgURL) is null. (This is when I run inside of eclipse, mind you, I havn't even exported it yet)
I figure it has something to do with my classpath,however I cannot figure out what.
Inside of eclipse, my package structure is as follows:
(Parent Directory, project name)
+src folder, has normal packages, etc, on build path
+resource folder, ("res"), not on build path (tried it on build path, nothing changed)
+++subdirectories of resource folder
Im at a loss of what to do here guys. Sorry for the wall of text.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码最初是在查找图像文件,而 Jar 文件中不存在文件,而只是资源。因此,您尝试使用 getClass.getResource("/res/client/BannerHeader.jpg"); 是正确的。但请确保该映像实际上位于类文件目录之外的目录中的 jar 文件中,并使用相对于类路径目录的路径。
该错误并不是告诉您这是错误的或文件为空,而是您需要将其放在 try/catch 块中。我建议您阅读异常教程以获取更多相关信息。
编辑 1
当你说你有一个未捕获的异常错误时,在我看来你有一个编译错误,而不是运行时异常。抱歉造成混乱。
至于放置图像的位置,在某种程度上取决于您使用的 IDE。我使用 Eclipse,并在 java 文件包目录中添加一个目录,我将其称为图像并将图像放置在那里。然后我使用资源来查找“images/MyImage.jpg”。
例如,假设我在 Eclipse 中的包如下所示:
因此类文件位于 myPackage 包中,图像文件 GridBoxClassPic.JPG 位于类文件目录之外的 images 目录中,因此我可以使用 String
String RESOURCE_PATH = 找到它“图像/GridBoxClassPic.JPG”;
。这段代码可以显示图像:
Your code was initially looking for image files, and files do not exist inside of a Jar file, just resources. So you were correct to try using
getClass.getResource("/res/client/BannerHeader.jpg");
. But make sure that the image is in fact in the jar file in a directory off of the class file directory, and use a path that is relative to the class path directory.The error isn't telling you that this is wrong or that the file is null, but rather you need to place this in a try/catch block. I suggest that you read the Exceptions Tutorial for more on this.
Edit 1
When you stated you had an uncaught exception error, it seemed to me that you had a compilation error, not a run-time exception. Sorry for the confusion.
As for where to place the images, it somewhat depends on what IDE you're using. I use Eclipse, and I add a directory off of my java file package directory that I call images and place my images there. I then use resources to look for "images/MyImage.jpg".
For example, say my packages in Eclipse look like so:
So the class files are located in the myPackage package, and the image file, GridBoxClassPic.JPG is located in the images directory off of the class file directory, so I can find it using the String
String RESOURCE_PATH = "images/GridBoxClassPic.JPG";
.This code could show the image:
好吧,如果您遵循方法一,我认为您只需要在 eclipse 中创建可运行的 jar 文件。然后,您需要将要加载的图像放在与 jar 文件相同的目录中,并具有与您在程序中提到的相同的相对路径。例如,
我认为那时它会起作用。
well, if you followed method one, I think you need to just create the runnable jar file in eclipse. Then you need to put the images you are loading in the same directory as the jar file, having the same relative path you had mentioned in your program. For example
I think it will work then.