包含带有可执行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
Java makes it easy to extract these "resources" from Jar files through the use of the ClassLoader
background = ImageIO.read(getClass().getResource("/wood.jpeg"));
应该有效......
Should work...
这个将返回 URL
, ImageIO
可用于加载资源。
This will return a URL
which ImageIO
can use to load the resource.
您还可以阅读
- Classpath resource within jar
- Jar get image as resource
- Load a resource in Jar
我可以列出更多内容。所以,是的,它被问了很多;)
And I could list some more. So, yeah, it gets asked a lot ;)
这篇关于包含带有可执行jar的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!