A Jar file is essentially a Zip file with a manifest.
Jar / Zip files do not actually have the concept of directories such as disks. They just have a list of entries with names. These names may contain some path separator, and some entries can be marked as directories (and usually do not have bytes associated with them, just acting as markers)
If you want to find all the resources in the given path, you will have to open the Jar file and check it yourself, for example ...
JarFile jf = null; try { String path = "resources"; jf = new JarFile(new File("dist/ResourceFolderCounter.jar")); Enumeration<JarEntry> entries = jf.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); if (!entry.isDirectory()) { String name = entry.getName(); name = name.replace(path + "/", ""); if (!name.contains("/")) { System.out.println(name); } } } } catch (IOException ex) { try { jf.close(); } catch (Exception e) { } }
Now you need to know the name of the Jar file that you want to use, this can be problematic, since you can list resources from several different banners ...
The best solution would be to create some kind of โresource search fileโ during the build, in which there would be all the resource names that you might need, perhaps even tied to specific names ...
That way you can just use ...
BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsInputStream("/resources/MasterResourceList.txt"))); String name = null; while ((name = br.readLine()) != null) { URL url = getClass().getResource(name); } } finally { try { br.close(); } catch (Exception exp) { } }
For instance...
You can even sow a file with the amount of resources;)
source share