Q:

How to get all resources inside a resource directory in a JAR file?

I'm using Spring Boot and I want to get resources.
Here's my directory structure:
├───java
│   └───...
├───resources
│   └───files
│       ├───file1.txt
│       ├───file2.txt
│       ├───file3.txt
│       └───file4.txt

I'm trying to get the resources in the files directory. Here's what I've done to access these files:
@Autowired
private ResourceLoader resourceLoader;

...
Stream<Path> walk = Files.walk(Paths.get(resourceLoader.getResource("classpath:files/").getURI()));

This works running locally when the files are in the target directory, but the files are not found when I run it from a JAR file. How do I fix this? I've checked that these files do exist in the jar located under BOOT-INF/classes/files.
Here's how maven is building and copying the resources into the JAR (I don't want the .txt files to be filtered):
<resources>
  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
    <excludes>
      <exclude>**/*.txt</exclude>
    </excludes>
  </resource>
  <resource>
    <directory>src/main/resources</directory>
    <filtering>false</filtering>
    <includes>
      <include>**/*.txt</include>
    </includes>
  </resource>
</resources>

A:

Can you try with the following code to read the files ?
ResourcePatternResolver resourcePatResolver = new PathMatchingResourcePatternResolver();
Resource[] AllResources = resourcePatResolver.getResources("classpath*:files/*.txt");
 for(Resource resource: AllResources) {
    InputStream inputStream = resource.getInputStream();
    //Process the logic
 }

This is not the exact solution for the code you have written, but it will give an outline about the resources to read.

