3

The following code grants me access to a specific PNG resource within my project:

BufferedImage temp = ImageIO.read(Thread.currentThread().getContextClassLoader().getResource("play.png"));

However, I have n images that I don't have to have to hardcode ("play.png", "pause.png", etc); without having an external image library (which I would simply loop through with File), is there a way to get all images within the project?

Ideally, all files within a specific source folder, really. Or source folders. getResources only seems to work for multiples of a specific named resource, which isn't what I want.

EDIT:

To be clear, this is a local project / application; it isn't intended to be a servlet or otherwise deployed on the Web. Question updated with the offline tag in case this is applicable.

Gorbles
  • 1,169
  • 12
  • 27
  • Possible duplicate of [How to get all the image resources in a project’s sub folders?](http://stackoverflow.com/questions/5996847/how-to-get-all-the-image-resources-in-a-project-s-sub-folders) – Mark Sholund Nov 10 '15 at 15:38
  • I did already check that answer, but that's in C# and this question is clearly in Java . . – Gorbles Nov 10 '15 at 15:42
  • http://stackoverflow.com/questions/5193786/how-to-use-classloader-getresources-correctly – IsidroGH Nov 10 '15 at 16:12
  • Folders are a GUI concept, where you seem to mean directories. But application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. Further to that, embedded resources are not open to being 'listed' in the way you seem to want. As an alternative, prepare a list as the Jars are being built, include the list in the Jar. – Andrew Thompson Nov 11 '15 at 00:13
  • @AndrewThompson - yeah, it does seem that way. Looks like the best alternative is to create an array (or similar) with matching names for all resources and iterate over that. – Gorbles Nov 11 '15 at 11:11

1 Answers1

1

You can do something like this.Here I am trying to get all the images in a folder named images in a servlet.

protected void doPost(HttpServletRequest request,
     HttpServletResponse response) throws ServletException, IOException {
     String pathToImages = getServletContext().getRealPath("/images");
     File[] files = new File(pathToImages).listFiles();
     showImages(files);
 }
 public static void showImages(File[] files) {
        for (File file : files) {
                System.out.println("File: " + file.getName());
            }
        }
RockAndRoll
  • 2,247
  • 2
  • 16
  • 35
  • I can see how that would work for you, but I'm dubious that `getServletContext()` would work within a local / offline application. Will test and get back to you. – Gorbles Nov 10 '15 at 16:34
  • if its a normal java project,you can try `String pathToImages ="src/images";` assuming images folder is under src. – RockAndRoll Nov 10 '15 at 16:49
  • There is a difference in how Java handles embedded resources that prevents this from working once deployed, but thanks regardless :) – Gorbles Nov 11 '15 at 11:12