The class below shows how to do it. Ultimately, you get the string
value from compiler.toSource() rather than from a method on Result, as
you might expect.
package com.bolinfest.closure;
import com.google.javascript.jscomp.CompilationLevel;
import com.google.javascript.jscomp.Compiler;
import com.google.javascript.jscomp.CompilerOptions;
import com.google.javascript.jscomp.JSSourceFile;
import com.google.javascript.jscomp.Result;
public class Main {
public static void main(String[] args) {
Compiler compiler = new Compiler();
CompilerOptions options = new CompilerOptions();
CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel
(options);
JSSourceFile extern = JSSourceFile.fromCode("externs.js",
"function alert(x) {}");
JSSourceFile input = JSSourceFile.fromCode("input.js",
"function hello(name) {" +
"alert('Hello, ' + name);" +
"}" +
"hello('New user');");
Result result = compiler.compile(extern, input, options);
if (result.success) {
System.out.println(compiler.toSource());