FileNotFoundException when loading freemarker template in java

后端 未结 5 1237
说谎
说谎 2021-01-04 07:40

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 webservi

5条回答
  •  孤城傲影
    2021-01-04 08:06

    Actually you're expected to specify absolute path (not relative) for dir where template is going to be place, see FreeMaker.Configuration:

    setDirectoryForTemplateLoading(java.io.File dir)
    Sets the file system directory from which to load templates.    
    Note that FreeMarker can load templates from non-file-system sources too. See setTemplateLoader(TemplateLoader) from more details.
    

    For instance, this is how to get template from src/test/resources/freemarker:

    private String final PATH = "src/test/resources/freemarker"
    // getting singleton of Configuration
    configuration.setDirectoryForTemplateLoading(new File(PATH))
    // surrounded by try/catch
    

提交回复
热议问题