Reputation: 1085
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?
Upvotes: 2
Views: 373
Reputation: 4277
Unfortunately, your code will not be reusable as-is in the case you want to explore your jar file automatically, because you just cannot create File
instances from the contents of a jar file. This is covered in this post: Unable to create File object for a file in Jar
There was also this similar question suggesting a possible solution: How do I list the files inside a JAR file?
Upvotes: 2