There is no way to simply get a filtered list of internal resources without first listing the contents of the Jar file.
Fortunately, this is actually not that difficult (and, fortunately, you did most of the hard work for me).
Basically, if you have a link to a JarFile , you just need to query its entries and JarFile over this list.
By checking the JarEntry name for the required match (i.e. resources ), you can filter out the necessary elements ...
For example...
import java.io.File; import java.io.IOException; import java.util.Enumeration; import java.util.jar.JarEntry; import java.util.jar.JarFile; public class ReadMyResources { public static void main(String[] args) { new ReadMyResources(); } public ReadMyResources() { JarFile jf = null; try { String s = new File(this.getClass().getResource("").getPath()).getParent().replaceAll("(!|file:\\\\)", ""); jf = new JarFile(s); Enumeration<JarEntry> entries = jf.entries(); while (entries.hasMoreElements()) { JarEntry je = entries.nextElement(); if (je.getName().startsWith("resources")) { System.out.println(je.getName()); } } } catch (IOException ex) { ex.printStackTrace(); } finally { try { jf.close(); } catch (Exception e) { } } } }
Caveat
This question is actually being asked. Instead, trying to read the contents of the Jar at runtime, it would be better to create some kind of text file containing a list of available resources.
This may be caused by the dynamic build process before creating the Jar file. It would be a much simpler solution to then read this file (e.g. via getClass().getResource() ) and then view the list of resources in a text file ... IMHO
Madprogrammer
source share