Images in jar file [duplicate]

一世执手 提交于 2021-01-29 10:50:37

问题


I made a java application and bundled all classes in jar file.When I run the project from eclipse my app is running successfully,but it is not fetching the images from the desired directory.Please let me know how can we present these image files to the end user (like we present .jar file)


回答1:


Probably you need to implement your own class loader to load embedded resources.




回答2:


It should work this way:

new ImageIcon(this.getClass().getClassLoader().getResource(imagePath))



回答3:


Try using getResourceAsStream() resp. getResource() in your code. That way you will be able to use the same code whether you decide to put your images in a jar or in a directory. A good explanation can be found here. Concerning the right path where to put your images during Eclipse development, follow this recommendation.



来源:https://stackoverflow.com/questions/17611546/images-in-jar-file

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