Java
Classpath
Programming
Resource Management
Code Tutorial

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:

java
1Enumeration<URL> resources = classLoader.getResources("com/example/resources");
2while (resources.hasMoreElements()) {
3    URL resource = resources.nextElement();
4    // Process each resource URL
5}

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:

java
1Path myDir = Paths.get(getClass().getClassLoader()
2                .getResource("com/example/resources").toURI());
3Files.walk(myDir)
4    .forEach(path -> System.out.println(path));

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:

java
1URL jar = getClass().getClassLoader().getResource("com/example/resources");
2JarURLConnection connection = (JarURLConnection) jar.openConnection();
3JarFile jarFile = connection.getJarFile();
4Enumeration<JarEntry> entries = jarFile.entries();
5while (entries.hasMoreElements()) {
6    JarEntry entry = entries.nextElement();
7    if (entry.getName().startsWith("com/example/resources")) {
8        // Process each entry
9    }
10}

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 FileNotFound exceptions.
  • Expect resource paths to differ between development (exploded directories) and production (JARs), and handle both.

Comparing the approaches

TechniqueSuitable ForLimitations
ClassLoader.getResourcesAll resourcesInefficient for JARs; only provides URLs not contents
File System WalkingResources in filesystem directoriesNot suitable for JAR-packed resources
JAR File ScanningResources packed inside JARsMore complex and labor-intensive

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data 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.

Practice algorithms