FileNotFoundException when loading freemaker template in Java
When loading freemaker template, even if the template actually exists in the path, I receive a file that can't find exception
Update: This is run as a web service It returns the XML to the client based on the search query When I call it from another Java program (from the static host), the template loads successfully However, when the client requests XML, a FileNotFoundException
Operating system: Windows 7 file absolute path: C: / users / Jay / workspace / WebService / templates/
This is my code:
private String templatizeQuestion(QuestionResponse qr) throws Exception { SimpleHash context = new SimpleHash(); Configuration config = new Configuration(); StringWriter out = new StringWriter(); Template _template = null; if(condition1) { _template = config.getTemplate("/templates/fibplain.xml"); } else if(condition2) { _template = config.getTemplate("/templates/mcq.xml"); } context.put("questionResponse",qr); _template.process(context,out); return out.toString(); }
Full error stack:
java.io.FileNotFoundException: Template /templates/fibplain.xml not found. at freemarker.template.Configuration.getTemplate(Configuration.java:495) at freemarker.template.Configuration.getTemplate(Configuration.java:458) at com.hm.newAge.services.Curriculum.templatizeQuestion(Curriculum.java:251) at com.hm.newAge.services.Curriculum.processQuestion(Curriculum.java:228) at com.hm.newAge.services.Curriculum.processQuestionList(Curriculum.java:210) at com.hm.newAge.services.Curriculum.getTest(Curriculum.java:122) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(UnkNown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(UnkNown Source) at java.lang.reflect.Method.invoke(UnkNown Source) at org.apache.axis2.rpc.receivers.RPCUtil.invokeServiceClass(RPCUtil.java:212) at org.apache.axis2.rpc.receivers.RPCMessageReceiver.invokeBusinessLogic(RPCMessageReceiver.java:117) at org.apache.axis2.receivers.AbstractInOutMessageReceiver.invokeBusinessLogic(AbstractInOutMessageReceiver.java:40) at org.apache.axis2.receivers.AbstractMessageReceiver.receive(AbstractMessageReceiver.java:114) at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:181) at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:172) at org.apache.axis2.transport.http.AxisServlet.doPost(AxisServlet.java:146) at javax.servlet.http.HttpServlet.service(HttpServlet.java:637) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(UnkNown Source)
Solution
The FreeMarker template path is resolved by the templateloader object, which should be specified in the configuration object The path you specify as the template path is interpreted by the templateloader and is usually relative to some basic directory (even if it starts with /), so it is also called the template root directory In your example, you didn't specify any templateloader, so you used the default templateloader, which is only backward compatible and almost useless (and dangerous) So do this:
config.setDirectoryForTemplateLoading(new File( "C:/Users/Jay/workspace/WebService/templates"));
next:
config.getTemplate("fibplain.xml");
Note that the template prefix does not exist because the template path is relative to C: / users / Jay / workspace / WebService / templates (this also means that the template cannot be exited with.. / - s, which may be important for security.)
You can also load templates from servvetcontext, classpath, etc., instead of from the real directory It all depends on the templateloader you choose
See: http://freemarker.org/docs/pgui_config_templateloading.html
Update: if you get FileNotFoundException instead of templatenotfoundexception, now upgrade FreeMarker to at least 2.3 It's time for 22 It also provides better error messages, just like the typical error when you use the default templateloader, it will tell you in the error message Less waste of developer time