I have written a java program using Freemarker template but it's showing deprecated error. And I'm not able to understand

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

public class HelloFreemarker {

    public static void main(String[] args)  
            throws IOException, TemplateException {
        Configuration cfg = new Configuration();
        cfg.setObjectWrapper(new DefaultObjectWrapper());
        cfg.setDirectoryForTemplateLoading(new File("."));
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("name", "World");
        Template template = cfg.getTemplate("hello.ftl");
        template.process(model, 
        new OutputStreamWriter(System.out));
    }
}

 hello ${name}!

I have written a java program using freemarker template.But it's showing configuration error when I try to compile/build the program. A message is showing the configuration as deprecated. I'm using jdk 8 and jre 8 and using eclipse neon as my ide. Please help me to execute the program

enter image description here

2 Answers

It happening because Configuration() constructor is deprecated starting from version 2.3.21.
Use new parameterized constructor Configuration(Version) for instantiating Configuration object.

As per freemarker API documentation Version is a class used to specify up to what freemarker version do you want to apply the fixes that are not 100% backward-compatible.

For example: Suppose I am using freemarker-2.3.28.jar and want to enable its all backward-compatible bugfixes/improvements then create Configuration objects as below

Configuration cofig = new Configuration(Configuration.VERSION_2_3_28);

All the freemarker API versions can be checked here. Hope this helps :)

It's probably because you do not specify a value for the configuraion manager when you create it. I've found an example on Apache's website:

Configuration cfg = new Configuration(Configuration.VERSION_2_3_25);

Apparently, when left blank it picks a default. That default then calls a depracated function.

You should figure out what version of the config you want to use; probably the latest stable.

Related