springboot web lost important function on Heroku, but work well in localhost

on my localhost, my web works fine like this: view run on localhost

the controller can bind the List of currencies to the select box normally. But when I deploy to Heroku, it looks like the controller never finds the right list to bind to thymeleaf. So the selection boxes are left blank: view run on heroku

here is my controller:

@Autowired MoneyConverter moneyConverter;
@GetMapping("/")
  public String renderForm(Model model) {
    model.addAttribute("moneyConvertRequest", new MoneyConvertRequest());
    List<CurrencyRate> currencies = moneyConverter.mapCurrencyRates();
    model.addAttribute("currencies", currencies);  
    return "form";
  }

and in the Service, my program reads files from a JSON and a CSV to parse data to a hashmap that link to Currency Data object like this:

private JsonNode masterNode;
private HashMap<String, CurrencyRate> mapCurrencyRates;

public MoneyConverter() {
    loadExchangeRateFromJSON();
    loadCurrencyCodeFromCSV();
  }

private void loadExchangeRateFromJSON() {
    try {
      File file = new ClassPathResource("static/exchange_rate.json").getFile();
      FileReader reader = new FileReader(file);
      BufferedReader bufferedReader = new BufferedReader(reader);
      ObjectMapper objectMapper = new ObjectMapper();
      masterNode = objectMapper.readTree(bufferedReader);
    } catch (FileNotFoundException e) {
      System.out.println(e);
    } catch (IOException e) {
      System.out.println(e);
    }
  }

  public void loadCurrencyCodeFromCSV() {
    mapCurrencyRates = new HashMap<>();
    try {
      File file = new ClassPathResource("static/currency.csv").getFile();
      CsvMapper mapper = new CsvMapper(); 
      CsvSchema schema = CsvSchema.emptySchema().withHeader(); 
      ObjectReader oReader = mapper.readerFor(CurrencyRate.class).with(schema); 
      Reader reader = new FileReader(file);
      MappingIterator<CurrencyRate> mi = oReader.readValues(reader); 

      while (mi.hasNext()) {
        CurrencyRate currencyRate = mi.next();
        String currencyCode = currencyRate.getCode();
        if (mapCurrencyRates.get(currencyCode) == null) { 
          float rate = getExchangeRate(currencyCode);  JSON
          if (rate > 0.0f) { 
            currencyRate.setRate(rate);
            mapCurrencyRates.put(currencyCode, currencyRate);
          }     
        }
      }
    } catch (IOException e) {
      System.out.println(e);
    }
  }

I have a wild guess that maybe there is mis-match between version of dependency between heroku environment and my localhost here is my pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>vn.techmaster</groupId>
    <artifactId>money</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>money</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-csv</artifactId>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>5.7.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>3.18.1</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Any suggestion how I can fix this error on Heroku. Thank you!

Answer

You should not read resources as files because when they are packed inside a jar file (like is the case when running on Heroku) this will no longer work.

As an alternative, read the files as a stream:

InputStream inputStream = getClass().getResourceAsStream(...)