java 包含带有可执行 jar 的图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12488836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Including Images with an executable jar
提问by Mintybacon
I have been browsing Stackoverflow all day looking for how to do this and I have not been successful yet
我一整天都在浏览 Stackoverflow 寻找如何做到这一点,但我还没有成功
I am packaging a quick game I made into a executable jar but I didnt reference the images correctly I just referenced the files
我正在将我制作的快速游戏打包成一个可执行的 jar 但我没有正确引用图像我只是引用了文件
background = ImageIO.read(new File("wood.jpeg"));
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
我在 src 默认包中有我的类我不确定我应该在哪里添加图像,或者我是否必须将它添加到构建路径或在最新版本的 eclipse 中将图像添加到构建路径的正确方法
回答by MadProgrammer
Files in a Jar are not files in the sense of a file on disk. They are simply a (possibly) compressed stream of bytes.
Jar 中的文件不是磁盘文件意义上的文件。它们只是一个(可能)压缩的字节流。
Java makes it easy to extract these "resources" from Jar files through the use of the ClassLoader
Java 通过使用 ClassLoader
background = ImageIO.read(getClass().getResource("/wood.jpeg"));
Should work...
应该管用...
This will return a URL
which ImageIO
can use to load the resource.
这将返回一个URL
它ImageIO
可以用来加载资源。
You could also have a read of
你也可以阅读
And I could list some more. So, yeah, it gets asked a lot ;)
我可以再列出一些。所以,是的,它被问了很多;)
回答by Kumar Vivek Mitra
Try using Constructing Runnable Jar
using the Eclipse.
尝试使用Constructing Runnable Jar
Eclipse。
R_Click on the Project in the Package Explorer --->
Export ---> Runnable JAR file --->
Select the option of Package required libraries into generated JAR