Search code examples
javascriptgoogle-app-enginegwtgoogle-closure-compiler

Using Google Closure's "compiler.jar" in Google App Engine


I am trying to use Google Closure Compiler API from my Java code. Function compile() receives the original source code, and returns the compiled source code in a String.

This code will run in Google App Engine, but when I deploy and run it I get an "server error". Without calling function below, I don't get any errors. At time of compilation I get warning "compiler.jar will not be available on server's classpath". Compiler.jar is the library I downloaded from Closure Compiler project website.

Any ideas of how to go around this?

Thanks a million,

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

Solution

  • At time of compilation I get warning "compiler.jar will not be available on server's classpath".

    You might have to move the compiler.jar to your WEB-INF/lib.

    this is likely the cause for the 500: if you don't deploy the compiler.jar as part of your webapp, your servlet (or whatever) will fail with a NoClassDefFoundError.