Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 201
CPA Avatar asked Mar 07 '17 18:03

CPA


1 Answers

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());
    }
like image 103
abcdefgh Avatar answered Oct 08 '22 20:10

abcdefgh