I am trying to display a number of BufferedImage
elements stored in an ArrayList<BufferedImage>
by first reading them from the disk to the arraylist then iterating across the arraylist. Since the images are organised into subfolders on the filesystem, I read them into the arraylist with
private void storeImages(final File folder) {
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
storeImages(fileEntry);// recursively iterates across all
// subdirectories in folder
} else {
try {
imageList.add(ImageIO.read(fileEntry));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
If I use JFileChooser
to select a directory on which to call storeImages
, then the code works fine. However, I want to package the images into an executable JAR file so that the images don't have to be distributed separately and so users don't have to find the place on disk where the images are stored. To try and read files from within the JAR, I tried
storeImages(new File(getClass().getResource("/images").toString());
but this throws a NullPointerException
. If push comes to shove, I can read the images from the filesystem, though I would prefer to read them from the JAR file. What exactly should I do to read the images from the JAR?