5

I have a Spring Boot application. The code that needs to access a file is under the /resources/templates directory.

The property from my application.properties file that I'm trying to read is given below:

pont.email.template.location=templates/mailTemplate.html

The above property that I'm trying to access via @Value in one of the @Component class is given below:

@Value("${pont.email.template.location}") 
private String templateLocation;

Now, I have this code given below which takes the template location and tries to read it via BufferedReader.

BufferedReader reader = new BufferedReader(new FileReader(templateLocation));

Now, the value containing the file location is getting picked up properly by @Value from application.properties.

But the problem is that the application is not able to find templates/mailTemplate.html.

The error that I'm getting is given below:

java.io.FileNotFoundException: templates/mailTemplate.html (No such file or directory)

Note: I have checked that mailTemplate.html is present under the /resources/templates directory.

How to resolve this issue?

8
  • may be, try classpath:templates/mailTemplate.html Commented Apr 8, 2019 at 11:07
  • Considering that the error happens when attempting to read the file, the code you posted right now is less relevant. Please share the code you use to read the file and add it to your question. Commented Apr 8, 2019 at 11:12
  • @Cooshal whit this path returns the same error: java.io.FileNotFoundException: classpath:templates/mailTemplate.html (No such file or directory) Commented Apr 8, 2019 at 11:13
  • @g00glen00b already done, thanks! Commented Apr 8, 2019 at 11:15
  • What is the exact location of this file? Is it src/main/resources/templates? Commented Apr 8, 2019 at 11:17

1 Answer 1

11

You cannot read a File from inside a JAR. This fails due to the fact that the File has to point to an actual file resource on the file system and not something inside a JAR.

Let Spring do the heavy lifting and use the Resource abstraction to hide the nasty internals. So instead of using a String use a Resource and prefix the value of the property with classpath: to make sure it is loaded from the classpath. Then use an InputStreamReader instead of FileReader to obtain the information you need.

@Value("${pont.email.template.location}") 
private Resource templateLocation;
----------------
BufferedReader reader = new BufferedReader(new InputStreamReader(templateLocation.getInputStream()));

In your application.properties prefix with classpath:.

pont.email.template.location=classpath:templates/mailTemplate.html

Now it should work regardless of the environment you are running in.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot! @M.Deinum this woks fine form me!
+1 this worked for me! i had to resolve the filename with file.getFilename() as in @Value("${file}") Resource file;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.