user411103
user411103

Reputation:

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

Upvotes: 2

Views: 1631

Answers (3)

donah
donah

Reputation: 113

try this:

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

Upvotes: 2

John
John

Reputation: 5468

If you haven't done so you need to disable threading in the compiler to run on app engine: see "disableThreads" in Compiler.java

http://code.google.com/p/closure-compiler/source/search?q=Compiler.java&origq=Compiler.java&btnG=Search+Trunk

Normally, the compiler spawns a new thread to be sure that it has a larger than standard stack.

Upvotes: 1

Thomas Broyer
Thomas Broyer

Reputation: 64551

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.

Upvotes: 1

Related Questions