0

Java version 11. The code doesnt show any errors. but run time show error Failed to execute CommandLineRunner

InputStream inputStream = TypeReference.class.getResourceAsStream("json/countrylist.json");

I add JSON file correctly resource directory. but this line retun null. How do I fix it.


import com.example.countryCRUD.model.Country;
import com.example.countryCRUD.repo.CountryRepository;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import java.util.List;

@Configuration
public class StudentConfigaration {

    @Bean
    CommandLineRunner commandLineRunner(CountryRepository countryRepository){

        return args->{
            ObjectMapper mapper = new ObjectMapper();
            TypeReference<List<Country>> typeReference = new TypeReference<List<Country>>(){};
            InputStream inputStream = TypeReference.class.getResourceAsStream("json/countrylist.json");

            

            try {
              List<Country> countryList = mapper.readValue(inputStream,typeReference);
                countryRepository.saveAll(countryList);
                System.out.println("Users Saved!");
            } catch (IOException e){
                System.out.println("Unable to save users: " + e.getMessage());
            }

        };
    }
}```



```Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-08-12 20:51:39.466 ERROR 17112 --- [           main] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:794) ~[spring-boot-2.5.3.jar:2.5.3]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:775) ~[spring-boot-2.5.3.jar:2.5.3]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:345) ~[spring-boot-2.5.3.jar:2.5.3]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) ~[spring-boot-2.5.3.jar:2.5.3]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) ~[spring-boot-2.5.3.jar:2.5.3]
    at com.example.countryCRUD.CountryCrudApplication.main(CountryCrudApplication.java:19) ~[classes/:na]
Caused by: java.lang.IllegalArgumentException: argument "src" is null
    at com.fasterxml.jackson.databind.ObjectMapper._assertNotNull(ObjectMapper.java:4757) ~[jackson-databind-2.12.4.jar:2.12.4]
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3592) ~[jackson-databind-2.12.4.jar:2.12.4]
    at com.example.countryCRUD.StudentConfigaration.lambda$commandLineRunner$0(StudentConfigaration.java:27) ~[classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:791) ~[spring-boot-2.5.3.jar:2.5.3]
    ... 5 common frames omitted```



Country class
```package com.example.countryCRUD.model;



import javax.persistence.*;
import java.io.Serializable;

@Embeddable
@Entity
public class Country implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(nullable = false, updatable = false)
    private Long id;
    private String name;

    public Country() {
    }

    public Country(Long id, String name) {
        this.id = id;
        this.name = name;
    }

    public Country(String name) {
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Country{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

Country.json [{"name":"Afghanistan"},{"name":"Åland Islands"},{"name":"Albania"},{"name":"Algeria"},{"name":"American Samoa"}]

ravindu
  • 1
  • 3
  • 1
    Does this answer your question? [Read file from resources folder in Spring Boot](https://stackoverflow.com/questions/44399422/read-file-from-resources-folder-in-spring-boot) – ImtiazeA Aug 12 '21 at 14:54
  • I try that way. but can't read JSON file sir – ravindu Aug 12 '21 at 15:17

2 Answers2

0

As you want to read from the classpath root you must use

InputStream inputStream = TypeReference.class.getClassLoader().getResourceAsStream("/json/countrylist.json");

TypeReference.class.getResourceAsStream will look in the directory where the class file is.

Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
0

Finally I changed my code like this. It will help someone.

    CommandLineRunner commandLineRunner(CountryRepository countryRepository){

        return args->{
            String json = "[{\"name\":\"Afghanistan\"},{\"name\":\"Åland Islands\"},{\"name\":\"Albania\"},{\"name\":\"Algeria\"},{\"name\":\"American Samoa\"},{\"name\":\"Andorra\"},{\"name\":\"Angola\"},{\"name\":\"Anguilla\"},{\"name\":\"Antarctica\"},{\"name\":\"Antigua and Barbuda\"},{\"name\":\"Argentina\"},{\"name\":\"Armenia\"},{\"name\":\"Aruba\"},{\"name\":\"Australia\"},{\"name\":\"Austria\"},{\"name\":\"Azerbaijan\"},{\"name\":\"Bahamas\"},{\"name\":\"Bahrain\"},{\"name\":\"Bangladesh\"},{\"name\":\"Barbados\"},{\"name\":\"Belarus\"},{\"name\":\"Belgium\"},{\"name\":\"Belize\"},{\"name\":\"Benin\"},{\"name\":\"Bermuda\"},{\"name\":\"Bhutan\"},{\"name\":\"Bolivia (Plurinational State of)\"},{\"name\":\"Bonaire, Sint Eustatius and Saba\"},{\"name\":\"Bosnia and Herzegovina\"},{\"name\":\"Botswana\"},{\"name\":\"Bouvet Island\"},{\"name\":\"Brazil\"},{\"name\":\"British Indian Ocean Territory\"},{\"name\":\"United States Minor Outlying Islands\"},{\"name\":\"Virgin Islands (British)\"},{\"name\":\"Virgin Islands (U.S.)\"},{\"name\":\"Brunei Darussalam\"},{\"name\":\"Bulgaria\"},{\"name\":\"Burkina Faso\"},{\"name\":\"Burundi\"},{\"name\":\"Cambodia\"},{\"name\":\"Cameroon\"},{\"name\":\"Canada\"},{\"name\":\"Cabo Verde\"},{\"name\":\"Cayman Islands\"},{\"name\":\"Central African Republic\"},{\"name\":\"Chad\"},{\"name\":\"Chile\"},{\"name\":\"China\"},{\"name\":\"Christmas Island\"},{\"name\":\"Cocos (Keeling) Islands\"},{\"name\":\"Colombia\"},{\"name\":\"Comoros\"},{\"name\":\"Congo\"},{\"name\":\"Congo (Democratic Republic of the)\"},{\"name\":\"Cook Islands\"},{\"name\":\"Costa Rica\"},{\"name\":\"Croatia\"},{\"name\":\"Cuba\"},{\"name\":\"Curaçao\"},{\"name\":\"Cyprus\"},{\"name\":\"Czech Republic\"},{\"name\":\"Denmark\"},{\"name\":\"Djibouti\"},{\"name\":\"Dominica\"},{\"name\":\"Dominican Republic\"},{\"name\":\"Ecuador\"},{\"name\":\"Egypt\"},{\"name\":\"El Salvador\"},{\"name\":\"Equatorial Guinea\"},{\"name\":\"Eritrea\"},{\"name\":\"Estonia\"},{\"name\":\"Ethiopia\"},{\"name\":\"Falkland Islands (Malvinas)\"},{\"name\":\"Faroe Islands\"},{\"name\":\"Fiji\"},{\"name\":\"Finland\"},{\"name\":\"France\"},{\"name\":\"French Guiana\"},{\"name\":\"French Polynesia\"},{\"name\":\"French Southern Territories\"},{\"name\":\"Gabon\"},{\"name\":\"Gambia\"},{\"name\":\"Georgia\"},{\"name\":\"Germany\"},{\"name\":\"Ghana\"},{\"name\":\"Gibraltar\"},{\"name\":\"Greece\"},{\"name\":\"Greenland\"},{\"name\":\"Grenada\"},{\"name\":\"Guadeloupe\"},{\"name\":\"Guam\"},{\"name\":\"Guatemala\"},{\"name\":\"Guernsey\"},{\"name\":\"Guinea\"},{\"name\":\"Guinea-Bissau\"},{\"name\":\"Guyana\"},{\"name\":\"Haiti\"},{\"name\":\"Heard Island and McDonald Islands\"},{\"name\":\"Holy See\"},{\"name\":\"Honduras\"},{\"name\":\"Hong Kong\"},{\"name\":\"Hungary\"},{\"name\":\"Iceland\"},{\"name\":\"India\"},{\"name\":\"Indonesia\"},{\"name\":\"Côte d'Ivoire\"},{\"name\":\"Iran (Islamic Republic of)\"},{\"name\":\"Iraq\"},{\"name\":\"Ireland\"},{\"name\":\"Isle of Man\"},{\"name\":\"Israel\"},{\"name\":\"Italy\"},{\"name\":\"Jamaica\"},{\"name\":\"Japan\"},{\"name\":\"Jersey\"},{\"name\":\"Jordan\"},{\"name\":\"Kazakhstan\"},{\"name\":\"Kenya\"},{\"name\":\"Kiribati\"},{\"name\":\"Kuwait\"},{\"name\":\"Kyrgyzstan\"},{\"name\":\"Lao People's Democratic Republic\"},{\"name\":\"Latvia\"},{\"name\":\"Lebanon\"},{\"name\":\"Lesotho\"},{\"name\":\"Liberia\"},{\"name\":\"Libya\"},{\"name\":\"Liechtenstein\"},{\"name\":\"Lithuania\"},{\"name\":\"Luxembourg\"},{\"name\":\"Macao\"},{\"name\":\"Macedonia (the former Yugoslav Republic of)\"},{\"name\":\"Madagascar\"},{\"name\":\"Malawi\"},{\"name\":\"Malaysia\"},{\"name\":\"Maldives\"},{\"name\":\"Mali\"},{\"name\":\"Malta\"},{\"name\":\"Marshall Islands\"},{\"name\":\"Martinique\"},{\"name\":\"Mauritania\"},{\"name\":\"Mauritius\"},{\"name\":\"Mayotte\"},{\"name\":\"Mexico\"},{\"name\":\"Micronesia (Federated States of)\"},{\"name\":\"Moldova (Republic of)\"},{\"name\":\"Monaco\"},{\"name\":\"Mongolia\"},{\"name\":\"Montenegro\"},{\"name\":\"Montserrat\"},{\"name\":\"Morocco\"},{\"name\":\"Mozambique\"},{\"name\":\"Myanmar\"},{\"name\":\"Namibia\"},{\"name\":\"Nauru\"},{\"name\":\"Nepal\"},{\"name\":\"Netherlands\"},{\"name\":\"New Caledonia\"},{\"name\":\"New Zealand\"},{\"name\":\"Nicaragua\"},{\"name\":\"Niger\"},{\"name\":\"Nigeria\"},{\"name\":\"Niue\"},{\"name\":\"Norfolk Island\"},{\"name\":\"Korea (Democratic People's Republic of)\"},{\"name\":\"Northern Mariana Islands\"},{\"name\":\"Norway\"},{\"name\":\"Oman\"},{\"name\":\"Pakistan\"},{\"name\":\"Palau\"},{\"name\":\"Palestine, State of\"},{\"name\":\"Panama\"},{\"name\":\"Papua New Guinea\"},{\"name\":\"Paraguay\"},{\"name\":\"Peru\"},{\"name\":\"Philippines\"},{\"name\":\"Pitcairn\"},{\"name\":\"Poland\"},{\"name\":\"Portugal\"},{\"name\":\"Puerto Rico\"},{\"name\":\"Qatar\"},{\"name\":\"Republic of Kosovo\"},{\"name\":\"Réunion\"},{\"name\":\"Romania\"},{\"name\":\"Russian Federation\"},{\"name\":\"Rwanda\"},{\"name\":\"Saint Barthélemy\"},{\"name\":\"Saint Helena, Ascension and Tristan da Cunha\"},{\"name\":\"Saint Kitts and Nevis\"},{\"name\":\"Saint Lucia\"},{\"name\":\"Saint Martin (French part)\"},{\"name\":\"Saint Pierre and Miquelon\"},{\"name\":\"Saint Vincent and the Grenadines\"},{\"name\":\"Samoa\"},{\"name\":\"San Marino\"},{\"name\":\"Sao Tome and Principe\"},{\"name\":\"Saudi Arabia\"},{\"name\":\"Senegal\"},{\"name\":\"Serbia\"},{\"name\":\"Seychelles\"},{\"name\":\"Sierra Leone\"},{\"name\":\"Singapore\"},{\"name\":\"Sint Maarten (Dutch part)\"},{\"name\":\"Slovakia\"},{\"name\":\"Slovenia\"},{\"name\":\"Solomon Islands\"},{\"name\":\"Somalia\"},{\"name\":\"South Africa\"},{\"name\":\"South Georgia and the South Sandwich Islands\"},{\"name\":\"Korea (Republic of)\"},{\"name\":\"South Sudan\"},{\"name\":\"Spain\"},{\"name\":\"Sri Lanka\"},{\"name\":\"Sudan\"},{\"name\":\"Suriname\"},{\"name\":\"Svalbard and Jan Mayen\"},{\"name\":\"Swaziland\"},{\"name\":\"Sweden\"},{\"name\":\"Switzerland\"},{\"name\":\"Syrian Arab Republic\"},{\"name\":\"Taiwan\"},{\"name\":\"Tajikistan\"},{\"name\":\"Tanzania, United Republic of\"},{\"name\":\"Thailand\"},{\"name\":\"Timor-Leste\"},{\"name\":\"Togo\"},{\"name\":\"Tokelau\"},{\"name\":\"Tonga\"},{\"name\":\"Trinidad and Tobago\"},{\"name\":\"Tunisia\"},{\"name\":\"Turkey\"},{\"name\":\"Turkmenistan\"},{\"name\":\"Turks and Caicos Islands\"},{\"name\":\"Tuvalu\"},{\"name\":\"Uganda\"},{\"name\":\"Ukraine\"},{\"name\":\"United Arab Emirates\"},{\"name\":\"United Kingdom of Great Britain and Northern Ireland\"},{\"name\":\"United States of America\"},{\"name\":\"Uruguay\"},{\"name\":\"Uzbekistan\"},{\"name\":\"Vanuatu\"},{\"name\":\"Venezuela (Bolivarian Republic of)\"},{\"name\":\"Viet Nam\"},{\"name\":\"Wallis and Futuna\"},{\"name\":\"Western Sahara\"},{\"name\":\"Yemen\"},{\"name\":\"Zambia\"},{\"name\":\"Zimbabwe\"}]";
            final ObjectMapper objectMapper = new ObjectMapper();
            List<Country> countryList = objectMapper.readValue(json, new TypeReference<List<Country>>(){});
            countryRepository.saveAll(countryList);
        };
    }```
ravindu
  • 1
  • 3