-1

This application should receive some code from HttpServletRequest, compile it, and return a file with the compiled code.

However, I get error below running the compiler.compile() call. No warnings or error messages were displayed at time of compilation in Eclipse.

Any suggestion please?

Thanks

public class MyServlet extends HttpServlet {

    public void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
    {
        res.setHeader("Content-Type", "text/javascript;charset=utf-8");
        res.setHeader("Content-Disposition","attachment;filename=compiled.js");
        PrintWriter printer=res.getWriter();
        String compiledCode=compile(req.getParameter("inputScript"));
        printer.write(compiledCode);
        printer.close();
    }

    public static String compile(String code)
    {
        com.google.javascript.jscomp.Compiler.setLoggingLevel(Level.INFO);
        com.google.javascript.jscomp.Compiler compiler = new com.google.javascript.jscomp.Compiler();

        compiler.disableThreads();
        CompilerOptions options = new CompilerOptions();
        CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
        JSSourceFile js = JSSourceFile.fromCode("input.js", code);
        WarningLevel.QUIET.setOptionsForWarningLevel(options);      
        compiler.compile(null, js, options);
        return compiler.toSource();
    }
}

ERROR:

java.lang.NullPointerException
    at com.google.javascript.jscomp.JsAst.<init>(JsAst.java:44)
    at com.google.javascript.jscomp.CompilerInput.<init>(CompilerInput.java:91)
    at com.google.javascript.jscomp.Compiler.makeCompilerInput(Compiler.java:381)
    at com.google.javascript.jscomp.Compiler.initModules(Compiler.java:341)
    at com.google.javascript.jscomp.Compiler.init(Compiler.java:317)
    at com.google.javascript.jscomp.Compiler.compile(Compiler.java:525)
    at com.google.javascript.jscomp.Compiler.compile(Compiler.java:491)

1 Answers1

0

It is simply that you need to provide a list of externs even if it is empty string, for the first parameter compiler.compile.

John
  • 5,443
  • 15
  • 21