我正在尝试从我的 Java 代码中使用 Google Closure Compiler API。函数compile()接收原始源代码,并以字符串形式返回编译后的源代码。
此代码将在 Google App Engine 中运行,但当我部署并运行它时,出现“服务器错误”。如果不调用下面的函数,我不会收到任何错误。在编译时,我收到警告“compiler.jar 将在服务器的类路径上不可用”。 Compiler.jar是我从Closure Compiler项目网站下载的库。
关于如何解决这个问题有什么想法吗?
感谢一百万,
import com.google.javascript.jscomp.*;
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();
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();
}
最佳答案
试试这个:
import com.google.javascript.jscomp.*;
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();
CompilerOptions options = new CompilerOptions();
CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
JSSourceFile js = JSSourceFile.fromCode("input.js", code);
List<SourceFile> list = new ArrayList<SourceFile>();
list.add(js);
WarningLevel.QUIET.setOptionsForWarningLevel(options);
compiler.compile(new ArrayList<SourceFile>(), list, options);
return compiler.toSource();
}
关于javascript - 在 Google App Engine 中使用 Google Closure 的 "compiler.jar",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9885719/