Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.1K
Views
Read files from BOOT-INF/classes

I have a Spring Boot application with a java resource folder:

src
 |
  main
   |
    resources
     |
      test
       |
        test1.json
        test2.json
        ...

In the resource folder there are json files. I can read these files within my IDE (IntelliJ). But as a compiled JAR file, I get Nullpointer exceptions.

Spring Boot copies the files to: BOOT-INF/classes/test Is it possible to read the resource files within a JAR file? I don't know the file names. So in first, I have to get all file names and the read each file.

Does anyone have an idea?

UPDATE

I have tried this:

Resources[] resources = applicationContext.getResources("classpath*:**/test/*.json");

With that I'm getting all file paths. But that needs too much time. And even if I get the file names, how would I read the files?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

This actually works using a ResourcePatternResolver

ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources("classpath*:test/*.json");

    for(Resource r: resources) {
        InputStream inputStream = r.getInputStream();
        File somethingFile = File.createTempFile(r.getFilename(), ".cxl");
        try {
            FileUtils.copyInputStreamToFile(inputStream, somethingFile);
        } finally {
            IOUtils.closeQuietly(inputStream);
        }
        LicenseManager.setLicenseFile(somethingFile.getAbsolutePath());
        log.info("File Path is " + somethingFile.getAbsolutePath());
    }
about 4 years ago · Santiago Trujillo Report

0

The below solution will read the files into a Map.

Here you read your resources:

Resource[] resources = applicationContext.getResources("classpath*:test/*.json"); 
for (Resource r: resources) {
     processResource(r);
}

Here you process your resources:

// you need to add a dependency (if you don't have it already) for com.fasterxml.jackson.core:jackson-databind
ObjectMapper mapper = new ObjectMapper();

private void processResource(Resource resource) {
    try {
          Map<String, Object> jsonMap = mapper.readValue(resource.getInputStream(), Map.class);

          // do stuffs with your jsoMap

        } catch(Exception e){
            e.printStackTrace();
        }
    }
}
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!