user277290
user277290

Reputation: 33

how to make jar file to load images on run time

i am loading buffered image in java through the following lines of code and it works good and when i clean and build this code .jar file is created which also loads the same file.but now if replace the picture in src folder the jar file still loading the old one picture. i want that once i clean and built my program the jar file can load images that are currently available. is there any solution for that or any other file extension that runs my program without using idle but loads images at run time.

      try {
        return ImageIO.read(imageloader.class.getResource(path));
      } catch (IOException ex) {
        Logger.getLogger(imageloader.class.getName()).log(Level.SEVERE, 
      null, ex);
     }

Upvotes: 0

Views: 91

Answers (2)

Lev Leontev
Lev Leontev

Reputation: 2615

This is how to load an image from file:

File file = new File("Pass the image file location here")
BufferedImage image = ImageIO.read(file)

Upvotes: 0

Highbrainer
Highbrainer

Reputation: 760

Using Class.getResource() loads file from the classpath. So I suppose the your jar contains the image file (because it is in the src folder), and your program loads the file from the jar. Not from an src folder anywhere else !

HTH,

Upvotes: 1

Related Questions