Putting all together

This is a working source file assembled from the previous fragments. Don't forget to put freemarker.jar into the CLASSPATH.

import freemarker.template.*;
import java.util.*;
import java.io.*;

public class Test {

    public static void main(String[] args) throws Exception {
        /* ----------------------------------------------------------------------- */    
        /* You should do this ONLY ONCE in the whole application life-cycle:       */    
        /* Create and adjust the configuration */
        Configuration cfg = new Configuration();

        cfg.setDirectoryForTemplateLoading(new File("/where/you/store/templates"));
        cfg.setObjectWrapper(new DefaultObjectWrapper());
        cfg.setIncompatibleImprovements(new Version(2, 3, 20));

        /* ----------------------------------------------------------------------- */    
        /* You usually do these for many times in the application life-cycle:      */    

        /* Create a data-model */
        Map root = new HashMap();
        root.put("user", "Big Joe");
        Map latest = new HashMap();
        root.put("latestProduct", latest);
        latest.put("url", "products/greenmouse.html");
        latest.put("name", "green mouse");

        /* Get the template */
        Template temp = cfg.getTemplate("test.ftl");

        /* Merge data-model with template */
        Writer out = new OutputStreamWriter(System.out);
        temp.process(root, out);


I have suppressed the exceptions for the sake of simplicity. Don't do it in real products.

FreeMarker Manual -- For FreeMarker 2.3.20
HTML generated: 2013-07-02 14:56:38 GMT
Edited with XMLMind XML Editor