Reputation: 394
I am using eclipse and I use the following code to load my image from a folder.
getClass().getResource("/images/image.jpg").getFile())
The image folder is located inside the bin folder in the project folder. It works fine when loading in eclipse, but when I export it to a jar it does not load. I have tried puting the image folder in all possible places in the jar, but it does not.
How do I load an image folder in a jar?
Upvotes: 0
Views: 2520
Reputation: 20653
You can use getResourceAsStream()
method instead to get InputStream
instance with your file data.
UPDATE: Loading of files from a jar happens with the help of class loader. And it can give you instance of InputStream (not FileInputStream) of any internal resource (be it image file or sound file or text file). File writing shouldn't work inside jar.
Upvotes: 2
Reputation: 11940
Use this. This method doesn't return immediately
.
public Image getImage(String img){
return new ImageIcon(getClass().getResource(img)).getImage();
}
If you want to load from the root of the JAR
, then
return new ImageIcon(getClass().getClassLoader().getResources(img)).getImage();
Upvotes: 1