Including Images with an executable jar

别说谁变了你拦得住时间么 提交于 2019-12-17 05:13:14

问题


I have been browsing Stackoverflow all day looking for how to do this and I have not been successful yet

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"));

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


回答1:


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 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...

This will return a URL which ImageIO can use to load the resource.

You could also have a read of

  • 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 ;)




回答2:


Try using Constructing Runnable Jar using the Eclipse.

R_Click on the Project in the Package Explorer ---> 
Export ---> Runnable JAR file ---> 
Select the option of Package required libraries into generated JAR


来源:https://stackoverflow.com/questions/12488836/including-images-with-an-executable-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!