2

我想從我的Java代碼中使用Google Closure Compiler API。函數compile()接收原始源代碼,並將編譯的源代碼返回到String中。在Google App Engine中使用Google Closure的「compiler.jar」

此代碼將在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(); 
} 
+1

「服務器錯誤」僅僅是個500級的用戶顯示 - 堆棧跟蹤是在你的服務器日誌(在管理控制檯中,在日誌)。那顯示了什麼? – 2012-03-27 09:25:08

回答

2

在編譯我得到警告的時間「compiler.jar將不提供對服務器的類路徑」。

您可能需要將compiler.jar移動到您的WEB-INF/lib

這可能是500的原因:如果你沒有將compiler.jar作爲web應用程序的一部分部署,那麼你的servlet(或其他)將會以NoClassDefFoundError失敗。

1

試試這個:

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(); 
} 
+0

可以使用此文件https://github.com/bramstein/closure-compiler-inline/blob/master/src/com/google/javascript/jscomp/JSSourceFile.java – ThinkTank 2017-10-24 06:29:32