javagoogle-app-enginegoogle-glassgoogle-mirror-api

Cannot instantiate the type configuration Mirror API


I recently purchased the Book "Programming Google Glass - The Mirror API" By Eric Redmond and in the 2nd chapter we install Freemarker GRE .jar file into the project. There is a part when we have to create a method that renders a template file. I keep getting an error when trying to make a Configuration.

package com.leetinsider.leetfoodfinder;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;
import java.util.Random;

import javax.security.auth.login.Configuration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import com.sun.org.apache.xalan.internal.xsltc.compiler.Template;

public class LeetFoodFinder {

    public static String getRandomCuisine()
    {
        String[] lunchOptions = {
                "American", "Chineese", "French", "Italian", "Japenese", "Thai"
        };
        int choice = new Random().nextInt(lunchOptions.length);
        return lunchOptions[choice];
    }
    public static String render(ServletContext ctx, String template, Map<String, Object> data) 
            throws IOException, ServletException{
            Configuration config = new Configuration();
            config.setServletContextForTemplateLoading(ctx, "WEB-INF/views");
            config.setDefaultEncoding("UTF-8");
            Template ftl = config.getTemplate(template);
            try{
                //use the data to render the template to the servlet output
                StringWriter writer = new StringWriter();
                ftl.process(data, writer);
                return writer.toString();
            }
            catch (TemplateException e){
                throw new ServletException("Problem while processing template", e);
            }
        }
}

It tells me that Configuration() cannot be instaniated. Is there an import that I am missing? I put the freemarker-gae2.3.2.0.jar file in the war/WEB-INF/lib directory but am not sure if there is something else I am missing.

Trying to follow along with the book but this is holding me back :/

enter image description here


Solution

  • If you look at your import statement, they're referring to non freemarker classes of the same name.

    1. The jar isn't actually in your build path. Right click the project and choose "Properties", then "Java Build Path". If freemarker isn't in the Libraries list, select Add JARs and find the jar in your project.
    2. Delete the "import javax.security.auth.login.Configuration" line. You need to choose the Freemarker configuration.

    Hope that helps.