0

I have the following line in my default.properties file :

json.countries = json/countries.json

I load the properties file in spring pretty easily, using the following :

@PropertySource(value="classpath:config/default.properties")

the json directory lives inside src/main/resources.

This makes the full path /src/main/resources/json/countries.json

I already know how to parse the JSON file etc, but no matter what the hell I seem to do, my application cannot see that file.

Anyone know how I can get this to work?

IWishIWasABarista
  • 413
  • 2
  • 4
  • 19
  • If the file is in classpath, you may refer to this http://stackoverflow.com/questions/4359876/how-to-load-reference-a-file-as-a-file-instance-from-the-classpath – PWC May 11 '17 at 03:38
  • Sorry, I am not sure I follow... What do you mean by "In classpath"? All I did was create a resources directory, then add a subdirectory named "json", and then added a file inside there named countries.json. Do I need to mess around with something else? – IWishIWasABarista May 11 '17 at 03:40
  • Simply "in classpath" means `countries.json` is in your jar file. the link is about how to read file from jar file . – PWC May 11 '17 at 03:44
  • Whoa, so hang on - if I want to do this, I can't just have the file be in there, run mvn package, and have it be in there? I have to put it in the jar afterwards? Damn - that isn't going to work for me then, this company doesn't allow introduction of external files after packaging. – IWishIWasABarista May 11 '17 at 03:49
  • Files in resources folder are automatically included in the ouput jar file by default in Maven. You can check this by open the jar file. – PWC May 11 '17 at 03:53
  • Ah yes - I see it in there actually!! But then how come when I do... File currFile = new File(getClass().getClassLoader().getResource("json/countries.json").getFile()); Object obj = parser.parse(new FileReader(currFile)); I am getting file not found exceptions? – IWishIWasABarista May 11 '17 at 04:00
  • Turns out that getFile() is incorrect when working with classpath. You need an inputstream instead. Now my problem is that I have no idea how to translate an inputStream to JSON. – IWishIWasABarista May 11 '17 at 04:38
  • Use InputStreamReader – PWC May 11 '17 at 04:45

0 Answers0