Friday, November 6, 2009

Calling the Closure Compiler from Java

Someone asked how to deal with Compiler input and output as strings on the closure-compiler-discuss Google group, so here is a code snippet that should help out:

* @param code JavaScript source code to compile.
* @return The compiled version of the code.
public static String compile(String code) {
Compiler compiler = new Compiler();

CompilerOptions options = new CompilerOptions();
// Advanced mode is used here, but additional options could be set, too.

// To get the complete set of externs, the logic in
// CompilerRunner.getDefaultExterns() should be used here.
JSSourceFile extern = JSSourceFile.fromCode("externs.js",
"function alert(x) {}");

// The dummy input name "input.js" is used here so that any warnings or
// errors will cite line numbers in terms of input.js.
JSSourceFile input = JSSourceFile.fromCode("input.js", code);

// compile() returns a Result, but it is not needed here.
compiler.compile(extern, input, options);

// The compiler is responsible for generating the compiled code; it is not
// accessible via the Result.
return compiler.toSource();

This is packaged in a runnable example in

Note that the recommended way of extending compiler behavior is to subclass CompilerRunner (most likely createOptions() will be the method you are interested in overriding). Your subclass should have a main() method and be set as the Main-Class in a jar. Such a jar can then be used with the -c option in the utility.


  1. Thanks a lot!
    This saved me 2 hours.

  2. Can you give me a hint/help to how can i also, generate the SourceMap from java??? I cant seem to find anyway, other than from the commandLine!!



  3. JavaScript is quite possibly the most utilized programming dialects on the planet that makes the page intelligent by having various systems and projects to play out a specific errand. Aurelia, ReactJS, EmberJS, NodeJS, among different systems stay the main apparatuses for building web applications. As per information, 95% of all destinations on the web were created with the utilization of JavaScript. With such an interest for this language, there are various offshore JavaScript developers to help you fabricate your group. "Recruit frontend designer Kyiv" is a significant basic solicitation on the web as Ukraine stays an alluring objective for West European nations for offshoring their devoted development groups. Web parts are significantly more modest in measure and don't need extra runtime as they are fundamentally program based. Web segments work on the vast majority of JS systems subsequently empower engineers of various mastery to create web parts. Another requesting system that shows its sensational interest organizations. Planned by Facebook, this structure is focused on worker side runtime for APIs. The structure recognizes the sorts and fields of customer's cooperations and prerequisites, and afterward gives specific capacities to those. ES-modules are remembered for the norm of ES6 that portrays crafted by modules inside the program. This innovation, subsequently, empowers designers to add and concentrate usefulness to a specific module which can be additionally downloaded through CDN.

  4. IoT-consulting helps companies to improve, because there is an individual approach to each and takes into account the wishes that often differ. Consulting firms on the Internet of Things can offer not only collection but also analysis, data transfer. Our company from Ukraine offers high-quality specialists who make a great contribution to the development of any company.

  5. Hello! Thank you so much for the information in this article about "Calling the Closure Compiler from Java". But I wanna say a few words about studying! I with one of my classmates are studying at the same college. In order to make his life so much happier and easier, I recommended him this writing company's website with examples of correct essays that can be used to do his own masterpiece. Maybe, you will need to take this help in the nearest future!!!