-1
此應用程序應該從HttpServletRequest接收一些代碼,對其進行編譯並返回帶有編譯代碼的文件。NullPointerException在Java代碼中集成Closure Compiler API
但是,在運行compiler.compile()調用時出現錯誤。在Eclipse中編譯時不顯示警告或錯誤消息。
有什麼建議嗎?
感謝
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();
}
}
錯誤:
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)