You can use a ResourcePatternResolver to get all the resources that match a particular pattern. For example:

Resource[] resources = resourcePatternResolver.getResources("/mydir/*.txt")

You can have a ResourcePatternResolver injected in the same way as ResourceLoader.

Answer from Andy Wilkinson on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 70410549 › classpath-file-directory-root-classpath-in-spring-boot-project › 70411646
java - Classpath file directory root (.classpath) in Spring-Boot project - Stack Overflow
I want to add a dependency manually inside .classpath file of my Spring-Boot project, but I can't find that file. Could you please tell me where the route is? This is my project structure:
🌐
Baeldung
baeldung.com › home › spring › access a file from the classpath in a spring application
Access a File from the Classpath using Spring | Baeldung
March 26, 2025 - Learn how to inject the contents of a resource file into our beans as a String, with Spring's Resource class making this very easy. ... The Resource interface helps in abstracting access to low-level resources. In fact, it supports the handling of all kinds of file resources in a uniform manner. Let’s start by looking at various methods to obtain a Resource instance. For accessing a resource from the classpath, we can simply use ClassPathResource:
🌐
Baeldung
baeldung.com › home › java › core java › get classpath from classloader in java
Get Classpath From ClassLoader in Java | Baeldung
October 7, 2024 - Once the program starts, any additional classloading will proceed by searching classes by their respective Internal Names, meaning, the fully qualified Class name with ‘.’ (dots) replaced by ‘/’ (slashes), as relative paths from the directory /tmp/program. ... Finally, we can specify any valid URL as classpath locations, not just file system paths.
🌐
Spring
docs.spring.io › spring-boot › docs › 1.0.1.RELEASE › reference › htmlsingle
Spring Boot Reference Guide
In summary: there is a spring script (spring.bat for Windows) in a bin/ directory in the .zip file, or alternatively you can use java -jar with the .jar file (the script helps you to be sure that the classpath is set correctly). GVM (the Groovy Environment Manager) can be used for managing multiple versions of various Groovy and Java binary packages, including Groovy itself and the Spring Boot CLI. Get gvm from http://gvmtool.net and install Spring Boot with
🌐
Spring
docs.spring.io › spring-framework › reference › core › resources.html
Resources :: Spring Framework
Therefore, in such cases you should prefer using classpath*: with the same Ant-style pattern, which searches all classpath locations that contain the com.mycompany base package: classpath*:com/mycompany/**/service-context.xml. A FileSystemResource that is not attached to a FileSystemApplicationContext (that is, when a FileSystemApplicationContext is not the actual ResourceLoader) treats absolute and relative paths as you would expect. Relative paths are relative to the current working directory, while absolute paths are relative to the root of the filesystem.
Find elsewhere
🌐
Spring
docs.spring.io › spring-framework › docs › 3.2.x › spring-framework-reference › html › resources.html
6. Resources
In this case, the resolution strategy is fairly simple: a ClassLoader.getResources() call is used on the last non-wildcard path segment to get all the matching resources in the class loader hierarchy, and then off each resource the same PathMatcher resolution strategy described above is used for the wildcard subpath. Please note that "classpath*:" when combined with Ant-style patterns will only work reliably with at least one root directory before the pattern starts, unless the actual target files reside in the file system.
🌐
Gradle
discuss.gradle.org › help/discuss
Adding a resource directory to the runtime class path and excluding it from the jar - Help/Discuss - Gradle Forums
December 12, 2024 - Greetings. I have a Java/Spring Boot project where I need to decrypt some sops encrypted .env files when the app starts up. The encrypted files are checked into the BitBucket repo, and I need to 1) use the Java class path to find the appropriate file, 2) decrypt it when the app starts using a fixed directory structure/naming convention, and 3) make sure that neither the encrypted or decrypted files are part of the Jar file that gets built in the project.
🌐
GitHub
github.com › spring-projects-experimental › spring-boot-thin-launcher › issues › 78
Ability to add additional external classpath directory · Issue #78 · spring-projects-experimental/spring-boot-thin-launcher
November 23, 2018 - spring-boot-thin-launch works great for my none spring app as well but I lose the ability the setup external classpath. Possible to add -Dthin.extraClassPath=xxxx ? This option allows me to configure my logback.xml location
Author   spring-projects-experimental
🌐
Spring
docs.spring.io › spring-framework › docs › current › javadoc-api › org › springframework › core › io › support › PathMatchingResourcePatternResolver.html
PathMatchingResourcePatternResolver (Spring Framework 7.0.8 API)
As of Spring Framework 6.0, if getResources(String) is invoked with a location pattern using the "classpath*:" prefix it will first search all modules in the boot layer, excluding system modules. It will then search the class path using ClassLoader APIs as described previously and return the ...
🌐
Medium
medium.com › @kouomeukevin › demystifying-the-classpath-in-spring-boot-why-and-how-to-use-it-c76ec31d8986
Demystifying the Classpath in Spring Boot: Why and How to Use it | by Kevin Kouomeu | Medium
April 6, 2024 - The Spring Boot classpath encompasses a collection of directories and JAR files, meticulously organized to facilitate class loading and resource retrieval.
🌐
Medium
medium.com › codex › accessing-resource-files-in-spring-boot-a-comprehensive-guide-eab958eb8f9c
Accessing Resource Files in Spring Boot: A Comprehensive Guide | by Igor Venturelli | CodeX | Medium
November 28, 2024 - In Spring Boot, resources are typically loaded from the classpath, meaning the path you specify must be relative to the classpath root. Understanding this relative path concept is critical because absolute paths will not work consistently across different environments (e.g., development vs. production). ... This path is relative to the src/main/resources directory...
🌐
Spring
docs.spring.io › spring-boot › docs › 1.0.1.RELEASE › reference › html › boot-features-external-config.html
21. Externalized Configuration
You can also refer to an explicit location using the spring.config.location environment property (comma- separated list of directory locations, or file paths). $ java -jar myproject.jar --spring.config.name=myproject ... $ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 360008117659-Run-a-spring-boot-application-with-test-resources-in-classpath
Run a spring boot application with test resources in classpath – IDEs Support (IntelliJ Platform) | JetBrains
And you can use this `--spring.config.location` in Program arguments of Run Configuration: <https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config> We do not recommend to change classpath at all, but still in case you are sure you need that, you may use one of the following methods at your own risk: 1. Hard - introduce synthetic module that will depend on the main module and include test properties 2. Simple but not reliable - add classes directory dependency to the module using Project structure and use src/test/resources there.
🌐
Initial Commit
initialcommit.com › blog › spring-read-files-from-classpath
Spring – Read files from classpath - Initial Commit
To read a resource file through an absolute path, simply use: Resource resource = new ClassPathResource("test.png"); To read it through a relative path, pass your current class as a second parameter: Resource resource = new ClassPathResourc...
🌐
Coderanch
coderanch.com › t › 740540 › frameworks › Fat-jar-unable-find-classpath
Fat jar unable to find classpath file inside /resources package (Spring forum at Coderanch)
I created a fat jar using Spring boot. Inside the bootstrap.yml I am trying to access a file which is under /resources only. location: classpath:secret.json But I am getting java.io.FileNotFoundException: class path resource [secret.json] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/Users/test/Downloads/Demo/demo-api/target/demo-api-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/secret.json The same is working in IDE.