Punter Vicky Punter Vicky - 1 year ago 141
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 Source
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());
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download