0

I want to use Velocity to template my emails, but i struggled to give the right path of my template : "template.vm" that in my resource folder created by maven.

I used : class.getResource("template.vm").getPath => but getResource retruns null wich is logique cause as i know template.vm in that case should be in the same package of the class, and my template.vm is on WEB-INF/classes

So i used : class.getResource("/template.vm").getPath, but it returns a wrong path, in my case the path of template.vm is : D:/folderOne/FolderTwo/WEB-INF/classes/template.vm and getResource retruns : /D:/folderOne/FolderTwo/WEB-INF/classes/template.vm what makes velocity class to throw : Unable to find resource with the given path : /D:/folderOne/FolderTwo/WEB-INF/classes/template.vm

I am struggling with this little probleme more then a day now, please help.

Badr DRAIFI
  • 335
  • 4
  • 12
  • Why do you need the path? Can't you just use the inputstream? – nitind Jul 04 '13 at 18:21
  • I need the path to give it to velocity methode to load the template, i use velocity like this : Template t = VelocityEngine.getTemplate(String name) when name is the name of the template : template.vm – Badr DRAIFI Jul 04 '13 at 18:32
  • @BadrDRAIFI its happening because u are tring to get the path from context, try this http://stackoverflow.com/questions/2308188/getresourceasstream-vs-fileinputstream – Akash Yadav Jul 04 '13 at 19:15
  • @AkashYadav i tried that, but the always the same problem ResourceNotFoundException – Badr DRAIFI Jul 04 '13 at 20:40

1 Answers1

1

You should use ClassPathResourceLoader instead of FileResourceLoader for your Velocity. If you use it, you can simply call VelocityEngine.getTemplate("template.vm").

Loša
  • 2,621
  • 2
  • 14
  • 19
  • What i am finding wierd, is why velocity execption throws : ResourceNotFoundException : Unable to find resource – Badr DRAIFI Jul 04 '13 at 20:45
  • 1
    This is because [Class.getResource](http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#getResource(java.lang.String)) returns `URL`. [URL.getPath](http://docs.oracle.com/javase/6/docs/api/java/net/URL.html#getPath()) does not do what you expect. It doesn't return file path. It returns path portion of the URL. Your URL is `file:///D:/FolderOne/FolderTwo/WEB-INF/classes/template.vm` and the path portion of the URL is `/D:/FolderOne/FolderTwo/WEB-INF/classes/template.vm`. – Loša Jul 04 '13 at 20:49