0

I have created json file inside resources dir in spring boot application. File path is like: resources/recipe/recipe.json When i use relative path, it won't be taken by java compiler so i'll get this exception. java.io.FileNotFoundException: recipe/recipe.json (No such file or directory). But this path is identified by IDE. if i click the path, it will be redirected to json file. Can anyone tell what the issue is ?

(FYI: absolute path is working perfectly)

Pubudu Jayasanka
  • 1,294
  • 4
  • 18
  • 34

3 Answers3

3

If you have the resources folder in your classpath, then you should use Spring's Resource classes and specify the resource location as

classpath:recipe/recipe.json

You probably want to read this answer: Spring Boot - Reading Text File using ResourceLoader

moilejter
  • 958
  • 8
  • 9
0

If your resouce is present inside resources/static/listing.csv

String path = "classpath:static/listings.csv";

ResultSet rs = new Csv().read(path, null, null);

Ritu Gupta
  • 2,249
  • 1
  • 18
  • 11
0

If you need to access a file which is not in the classpath, you can provide the relative path from the root module directory.

E.g.

new File("src/main/resouces/recipe/recipe.json");

Prasad Karunagoda
  • 2,048
  • 2
  • 12
  • 16