Can I list the resources in a given package?

Andreas Petersson picture Andreas Petersson · Oct 9, 2008 · Viewed 19.6k times · Source

Lets assume my classes are loaded from a compressed .war file or loaded elsewhere, how can I discover all the resources in a given package? Enumerating files will not really work, since this is a war file. Most likely this will involve using the current classloader?

Is there a library out there that does something like that? Googling revealed only some hacks with listing files.

Answer

jcfolsom picture jcfolsom · Aug 18, 2012

Yes. You can use ClassLoader.getResources("");

Or you could use ferret API:
https://www.ohloh.net/p/pureperfect-ferret

This API allows you to scan the class path as well as arbitrary archives and directories using the Visitor pattern.