java io FileNotFoundException Template ftl not found
It is very common issue while working with FreeMarker template created *.ftl not found. First thing we should do is to set classpath wherever we kept template file.
java.io.FileNotFoundException: Template “JavaHonkFreeMarkerTemplate.ftl” not found
- Solution: Before using FreeMarker template you will have to set template loader and give path from where FreeMarker will load the template. In below example I have include how to set template loader and in case one path fail you could use another and example I am catching exception and loading another in catch block (It’s not preferable). In real time you definitely should know the actual path where file is available.
FreeMarketTemplateLoad.java:
package com.wfs.otc.report.util; import java.io.File; import java.io.IOException; import freemarker.cache.FileTemplateLoader; import freemarker.template.Configuration; import freemarker.template.Template; public class FreeMarketTemplateLoad { public static void main(String[] args) throws IOException { Configuration freemarkerConfiguration = new Configuration(); FileTemplateLoader templateLoader = null; try { templateLoader = new FileTemplateLoader(new File("..\\config\\")); freemarkerConfiguration.setTemplateLoader(templateLoader); } catch (Exception e) { try { templateLoader = new FileTemplateLoader(new File("src/main/resources")); freemarkerConfiguration.setTemplateLoader(templateLoader); } catch (IOException e1) { e1.printStackTrace(); } } Template template = freemarkerConfiguration.getTemplate("Javahonk.ftl"); System.out.println("Template loaded successfully and name is: "+template.getName()); } }
- Output:
- For more information please visit FreeMarker site here