问题
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