包含带有可执行 jar 的图像 [英] Including Images with an executable jar
问题描述
我一整天都在浏览 Stackoverflow 寻找如何做到这一点,但我还没有成功
I have been browsing Stackoverflow all day looking for how to do this and I have not been successful yet
我正在将我制作的快速游戏打包成一个可执行的 jar 但我没有正确引用图像我只是引用了文件
I am packaging a quick game I made into a executable jar but I didnt reference the images correctly I just referenced the files
background = ImageIO.read(new File("wood.jpeg"));
我在 src 默认包中有我的类我不确定应该在哪里添加图像,或者是否必须将其添加到构建路径中或在最新版本的 eclipse 中将图像添加到构建路径的正确方法
I have my classes in src default package Im not sure where I should add the images or if I have to add it to the build path or correct way of adding the images to the build path in the newest version of eclipse
推荐答案
Jar 中的文件不是磁盘上的文件.它们只是一个(可能)压缩的字节流.
Files in a Jar are not files in the sense of a file on disk. They are simply a (possibly) compressed stream of bytes.
Java 通过使用 ClassLoader
background = ImageIO.read(getClass().getResource("/wood.jpeg"));
应该可以...
这将返回一个 URL
,ImageIO
可以使用它来加载资源.
This will return a URL
which ImageIO
can use to load the resource.
你也可以阅读
我可以列出更多.所以,是的,它被问了很多;)
And I could list some more. So, yeah, it gets asked a lot ;)
这篇关于包含带有可执行 jar 的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!