Punter Vicky Punter Vicky - 1 month ago 27
Java Question

Spring Boot - Identify resources under resource folder

I have a Spring Boot App. I am trying to read few files that I have placed under main/resources folder. I see that Spring Boot automatically reads application.properties under resources folder. However it doesn't seem to read other xml / text files that I have placed under resources folder.

Source xslt = new StreamSource(new File("removeNs.xslt"));


Should I add any additional configuration for the program to automatically read all the files under resources folder without having to explicitly provide the path?

Answer
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("removeNs.xslt").getFile());

This should work.

Thanks @Edwin for mentioning a more robust solution:

File file = new File(Thread.currentThread().getContextClassLoader().getResource("removeNs.xslt").getFile());
Comments