Alec Porter wrote:The problem is that the jar isn't created till I make the code into a jar file that means there would be no way to test it in the compiler, should I make a separate jar file for images? I wanted to have the images in the same jar as the main class so I could load them like I did in the code. Is it not possible to get the listFiles() to work because their in the jar?
Normally the way that people do things, when they want to distribute their project as a jar which includes resources like images, is to use the Class.getResource() method to retrieve a resource. You've already seen that in this
thread and you already know how to make it work when the resource is in the jar. So far so good.
Now, the way you make it work in your IDE is to realize that the getResource() method gets resources
from the classpath. So that means, in your example, that you should put an "images" directory in your project's build path and then put the images in it. Sorry I can't be more specific, but how you do that depends on what your IDE is and even on how your project is set up in the IDE. But once you have that done, then Class.getResource() will find the images when you run the code in your IDE. If you do it right, then you can even ask your IDE to create a jar and it will automatically include your "images" directory.
So that's when you know the name of your resource. But where you're going outside of normal practice is that you're designing a system with a collection of resources of which you don't know all the names. And the getResource() method doesn't have a way of returning a list of URLs which is comparable to the File.listFiles() method. That's where you get into hacks like finding the URL of the jar file and using other code which then looks into the jar to get the resources. Unfortunately, as you see, this prevents you from testing in your IDE.
What I would recommend is that you create a resource which is a text file containing the names of all the images in your "image" directory. Then your first step is to use getResource() to get this text file, and then you can loop through it and get all of your image resources, also using getResource().
This simplifies your programming and testing tasks, at the expense of requiring you to maintain an extra resource which is somewhat redundant.