0

I have some code in a Spring application that loads a file from a sub package (foo.bar.resources) of the current package:

package foo.bar

InputStream in = this.getClass().getResourceAsStream("./resources/facility.xml");

I would like to be able to move the xml file to the conventional resources folder but are unsure how to load it without using an absolute path

skyman
  • 2,255
  • 4
  • 32
  • 55
  • What is the exception ? FileNotFound ?? or something else – Anupam Biswas Jan 18 '18 at 08:24
  • i doubt if there is any way to do this since we have to declare path – spandey Jan 18 '18 at 08:27
  • Where's the "conventional resources folder"? Still inside the jar? – tom Jan 18 '18 at 08:33
  • Can you try using: `this.getClass().getClassLoader().getResourceAsStream(..)` – Andre Albert Jan 18 '18 at 08:33
  • There is no exception. The code as written works, however I would like to move to the xml file to the resources folder (or somewhere else on the classpath) and not have to specify an absolute path - maybe it is not possible? – skyman Jan 18 '18 at 08:36
  • If your file is in `procject\src\resources\file.xml` you should use `getClass().getClassLoader().getResourceAsStream("file.xml");`. If your file is in other project folder (not in `/src`) - use `new File(System.getProperty("user.dir") + "\\folder\\file.xml");`. I have tried both methods and both's works for me. – Dumbo Jan 18 '18 at 11:21

0 Answers0