Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

What is the difference between classpath: and classpath*: in Spring resource locations, and when do you need the second one?

A classpath: location returns the first matching resource the class loader finds, while classpath*: returns every matching resource from every jar and directory on the classpath. We need classpath*: when several modules contribute files with the same name, such as META-INF files or SQL scripts spread over jars. Wildcards such as classpath*:db/*.sql are expanded by PathMatchingResourcePatternResolver, and a pattern that starts at the classpath root without a base directory may miss files inside jars, so we always give it a directory such as db/.

Why this question is asked

Resource locations look like small strings in a configuration file, but the prefix in front of them decides how many files Spring finds. A missing star can mean that only one of five modules' migration scripts runs, or that a plugin's configuration is silently ignored, and the application starts without any error. Interviewers ask this because the bug is quiet and the explanation shows whether we understand how the Java class loader looks up resources. In this answer, we will recap what the classpath is, see what classpath: returns, see what classpath*: returns, look at wildcard patterns and the root-directory trap, and decide when each prefix is right.

1A Quick Recap of the Classpath

The classpath is the list of places the JVM looks for classes and other files: directories such as target/classes, and jar files such as each dependency. A resource is any non-class file on it, like application.yml, a template or an SQL script. The same relative path, for example META-INF/plugin.properties, can exist in several of those places at once, one copy in each jar.

Spring reads resources through its Resource abstraction, and a location string with a prefix tells it how to find them. The classpath prefixes delegate to the class loader, which offers two methods: getResource(name), which returns the first match in classpath order, and getResources(name), which returns all of them.

2What classpath: Returns

A classpath: location maps to getResource. The class loader walks the classpath in order and stops at the first entry that contains the path. Every other copy is invisible.

Java
// Three jars each contain META-INF/plugin.properties
ResourceLoader loader = new DefaultResourceLoader();
Resource r = loader.getResource("classpath:META-INF/plugin.properties");
System.out.println(r.getURL());
// jar:file:/libs/plugin-a.jar!/META-INF/plugin.properties (only the first one)

For a single, unique file such as our own application template, this is exactly what we want. For files that several modules provide, it is a bug, and which copy wins depends on classpath order, which can change when dependencies change.

3What classpath*: Returns

A classpath*: location maps to getResources and collects every match from every jar and directory. Because it can return many resources, we use a ResourcePatternResolver, the extension of ResourceLoader that returns an array. Every ApplicationContext is one, and PathMatchingResourcePatternResolver is the standard implementation.

Java
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] all = resolver.getResources("classpath*:META-INF/plugin.properties");
System.out.println(all.length); // 3
// jar:file:/libs/plugin-a.jar!/META-INF/plugin.properties
// jar:file:/libs/plugin-b.jar!/META-INF/plugin.properties
// jar:file:/libs/plugin-c.jar!/META-INF/plugin.properties
The same path in three jars
LocationLookup usedResult
classpath:META-INF/plugin.propertiesgetResource, first matchplugin-a only
classpath*:META-INF/plugin.propertiesgetResources, all matchesplugin-a, plugin-b, plugin-c
classpath:db/*.sqlfirst db/ root, then patternscripts from one jar only
classpath*:db/*.sqlevery db/ root, then patternscripts from every jar

This is the mechanism behind many plugin-style features. Spring Boot's own auto-configuration discovery, for example, reads files under META-INF/spring from every jar, because each starter contributes its own copy.

4How Wildcard Patterns Are Resolved

Patterns with wildcards, such as classpath*:db/*.sql or classpath*:db/**/*.sql, are handled by PathMatchingResourcePatternResolver. It splits the pattern into a root directory without wildcards, here db/, and the rest. It asks the class loader for every location of that root, which works for both directories and jars, and then walks each root, matching file names against the pattern with Ant-style rules: * matches within one directory level and ** matches any number of levels.

Notice what happens with classpath: plus a wildcard. The resolver finds only the first db/ root and searches inside it, so scripts in other jars are skipped, without any warning.

5The Root Directory Trap

There is one more catch. A pattern whose root is the classpath root itself, such as classpath*:*.sql, asks the class loader for the locations of the empty path. Class loaders usually return only the directories on the classpath for that, not the roots of jar files, so files at the top level of jars are missed. The fix is to always put resources in a base directory and use it in the pattern.

Java
resolver.getResources("classpath*:*.sql"); // may find only files in target/classes
resolver.getResources("classpath*:db/*.sql"); // finds db/*.sql in directories and in every jar

So the rule of thumb is: shared resources live in a named directory, ideally one specific to the module, such as db/migration/orders, and patterns always start from a directory.

6Follow-up questions

When should I use classpath: rather than classpath*:?

When the resource is unique to our application and we want exactly one, such as a template or a keystore. classpath: then fails clearly when the file is missing, whereas classpath*: simply returns an empty array, which can hide a mistake.

Does classpath*: work with ResourceLoader.getResource()?

No. getResource returns a single Resource, so a classpath*: location needs ResourcePatternResolver.getResources. Injecting Resource[] with @Value("classpath*:db/*.sql") also works, because the application context resolves the pattern.

What order are classpath*: results in?

They follow classpath order, then the order in which the resolver walks each root. That order is not something to rely on, so if order matters, as for SQL scripts, sort the resources by file name or use a migration tool that orders by version.

7Conclusion

In this answer, we saw that classpath: uses the class loader's getResource and returns only the first match, while classpath*: uses getResources and returns matches from every jar and directory. We need classpath*: whenever several modules contribute files with the same name, such as META-INF files or SQL scripts. Wildcard patterns are expanded by PathMatchingResourcePatternResolver from a root directory, and patterns at the classpath root can miss jar contents, so we always start them from a base directory.

What to say in the interviewAbout 90 seconds aloud

Open with

The difference is how many resources come back. classpath: uses the class loader's getResource, which walks the classpath in order and returns the first match, so if three jars each have META-INF/plugin.properties, I only get the one from whichever jar comes first. classpath*: uses getResources and returns every match from every jar and directory, which is what I need whenever several modules contribute a file with the same name, like META-INF files, plugin descriptors or SQL scripts.

Then, if they want more

Spring Boot's own auto-configuration discovery works like that. Wildcards such as classpath*:db/*.sql are expanded by PathMatchingResourcePatternResolver: it takes the part without wildcards, db/, finds every location of that directory, in folders and inside jars, and matches the rest of the pattern with Ant-style rules.

If I wrote classpath: with a wildcard, it would search only the first db/ it finds, and scripts in other jars would be silently skipped. The other trap is a pattern at the classpath root, like classpath*:*.sql, because class loaders usually do not return jar roots for the empty path, so top-level files in jars are missed.

That is why I always put shared resources in a base directory and start the pattern from it. And for a unique resource I prefer classpath:, because it fails clearly if the file is missing.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is the difference between classpath: and classpath*: in Spring resource locations...": https://scrivio-interview-library.netlify.app/spring-boot/spring-utilities/d9c5bab1fd/