Loading Resources in Runnable Jar

So, I have the following code in a method to load all PNG files found in a specific directory as textures.


String files;
File folder = new File(GRAPHICS_FILE_PATH);
File[] listOfFiles = folder.listFiles(); 
			  
for (int i = 0; i < listOfFiles.length; i++) 
{			  
	    if (listOfFiles[i].isFile()) 
	    {
	    	files = listOfFiles[i].getName();
	        if (files.endsWith(".png") || files.endsWith(".PNG"))
	        {			           
	           Texture temp = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream(GRAPHICS_FILE_PATH + "/" + files));
	           texture.put(files.substring(0, files.indexOf(".")), temp);
                }
	     }
}

This works great when testing in the IDE, but I want to distribute my final program as a runnable jar. Since there is only a single jar file and not this blown up file structure, the program freaks out saying it can’t find the specified resource.

If I hard code the texture names, slick’s ResourceLoader class works great in a jar or when the file structure exists; where’s the fun in hard-coding all those texture filenames though? This is programming; we want things done automatically! :wink:

My question is: is there a simple way to retrieve all the resources at a specific path in either a jar or file structure? I found this method, but I’m not having much luck implementing it in a way I can just point to “res/graphics”; it seems to depend on the same folder where the provided class exists.

  • Steve