FileNotFoundException when loading freemarker template in java

Tags: , , ,

I get a file not found exception while loading a freemarker template even though the template is actually present in the path.

Update: This is running as a webservice. It will return an xml to the client based on a search query. The template loads successfully when i call it from another java program(from static main). But the when the client requests for the xml, FileNotFoundException occurs.

OS: Windows 7 Absolute path of file: C:/Users/Jay/workspace/WebService/templates/

Here 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;

        _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: Template /templates/fibplain.xml not found.
at freemarker.template.Configuration.getTemplate(
at freemarker.template.Configuration.getTemplate(
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(
at org.apache.axis2.rpc.receivers.RPCMessageReceiver.invokeBusinessLogic(
at org.apache.axis2.receivers.AbstractInOutMessageReceiver.invokeBusinessLogic(
at org.apache.axis2.receivers.AbstractMessageReceiver.receive(
at org.apache.axis2.engine.AxisEngine.receive(
at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(
at org.apache.axis2.transport.http.AxisServlet.doPost(
at javax.servlet.http.HttpServlet.service(
at javax.servlet.http.HttpServlet.service(
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
at org.apache.catalina.core.ApplicationFilterChain.doFilter(
at org.apache.catalina.core.StandardWrapperValve.invoke(
at org.apache.catalina.core.StandardContextValve.invoke(
at org.apache.catalina.core.StandardHostValve.invoke(
at org.apache.catalina.valves.ErrorReportValve.invoke(
at org.apache.catalina.core.StandardEngineValve.invoke(
at org.apache.catalina.connector.CoyoteAdapter.service(
at org.apache.coyote.http11.Http11Processor.process(
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(
at Source)


FreeMarker template paths are resolved by a TemplateLoader object, which you should specify in the Configuration object. The path that you specify as the template path is interpreted by the TemplateLoader, and is usually relative to some kind of base directory (even if it starts with /), that’s also called the template root directory for this reason. In your example, you haven’t specified any TemplateLoader, so you are using the default TemplateLoader, which is only there for backward-compatibility, and is nearly useless (and also dangerous). So, do something like this:

config.setDirectoryForTemplateLoading(new File(

and then:


Note that the /template prefix is not there now, as the template path is relative to C:/Users/Jay/workspace/WebService/templates. (This also means that the template can’t back out of it with ../-s, which can be important for security.)

Instead of loading from a real directory, you can also load templates from a SerlvetContext, from the “class path”, etc. It all depends on what TemplateLoader you are choosing.

See also:

Update: If you get FileNotFoundException instead of TemplateNotFoundException, it’s time to upgrade FreeMarker to at least 2.3.22. It also gives better error messages, like if you do the typical mistake of using the default TemplateLoader, it tells you that right in the error message. Less wasted developer time.

Source: stackoverflow