Java: list resources in a classpath directory or JAR
Codemia Knowledge Hub ·
Java has no single API that lists the contents of a classpath directory, because directories are not resources in their own right. You get a URL for the directory from the ClassLoader, then list it one of two ways: walk the file system with NIO when the resources sit in a plain directory, or scan the entries of the JAR when they are packaged.
What the classpath contains
The classpath, set on the command line, through an environment variable or in an IDE, tells Java where to find user-defined classes and packages. Its entries can be directories, JAR archives and ZIP files. Resources such as configuration files and templates live alongside the classes in those locations.
Reading one known resource is easy with getResourceAsStream(String name). Listing everything under a directory takes more work.
Getting URLs with ClassLoader.getResources()
getResources(String name) returns an enumeration of URL objects, one for each classpath location that contains the path:
This only gives you URLs. You still have to work out whether each one points at a directory or into a JAR and list its contents accordingly.
Walking a directory on the file system
When the resources are in a normal directory (not inside a JAR), Java NIO can walk it:
This fails once the resources are packed into a JAR, because the URL no longer maps to a file system path.
Scanning entries inside a JAR
For resources packed in a JAR, open the JAR through its connection and filter the entries by prefix:
Practical advice
- Keep resources in a consistent directory structure so the prefix you scan for is predictable.
- Catch and handle
IOException, since any of these I/O calls can fail. - Validate paths and check that files exist to avoid
FileNotFoundexceptions. - Expect resource paths to differ between development (exploded directories) and production (JARs), and handle both.
Comparing the approaches
| Technique | Suitable For | Limitations |
ClassLoader.getResources | All resources | Inefficient for JARs; only provides URLs not contents |
| File System Walking | Resources in filesystem directories | Not suitable for JAR-packed resources |
| JAR File Scanning | Resources packed inside JARs | More complex and labor-intensive |
Related reading
- Get a sublist from an ArrayList efficiently
- Get an OutputStream into a String
- Get command-line arguments from spring-bootrun
- Get first and last day of month using threeten, LocalDate
- Get first or last entry of a LinkedHashMap
- Get generic type of java.util.List
- Get integer value of the current year in Java
- Get java.nio.file.Path object from java.io.File
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.