Stay organized with collections Save and categorize content based on your preferences.
The Hello World of the Closure Compiler ApplicationThe Closure Compiler application is a Java command-line utility that compresses, optimizes, and looks for mistakes in your JavaScript. To try out the Closure Compiler application with a simple JavaScript program, follow the steps below.
To work through this exercise you need the Java Runtime Environment version 7.
Download the Closure Compiler package
Create a working directory called closure-compiler
.
Download the most recently released JAR file from the Maven repository, and save it in closure-compiler
.
Create a JavaScript file
Create a file named hello.js
containing the following JavaScript:
// A simple function. function hello(longName) { alert('Hello, ' + longName); } hello('New User');
Save this file in the closure-compiler
directory.
Compile the JavaScript file
Run the following command from the closure-compiler
directory:
java -jar compiler.jar --js hello.js --js_output_file hello-compiled.js
This command creates a new file called hello-compiled.js
, which contains the following JavaScript:
function hello(a){alert("Hello, "+a)}hello("New User");
Note that the compiler has stripped comments, whitespace and an unnecessary semi-colon. The compiler has also replaced the parameter name longName
with the shorter name a
. The result is a much smaller JavaScript file.
To confirm that the compiled JavaScript code still works correctly, include hello-compiled.js
in an HTML file like this one:
<html> <head><title>Hello World</title></head> <body> <script src="hello-compiled.js"></script> </body> </html>
Load the HTML file in a browser, and you should see a friendly greeting!
This example illustrates only the most simple optimizations performed by the Closure Compiler. To learn more about the compiler's capabilities, read Advanced Compilation and Externs.
To learn more about other flags and options for the Closure Compiler, execute the jar with the --help
flag:
java -jar compiler.jar --help
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2023-10-25 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2023-10-25 UTC."],[[["The Closure Compiler is a Java command-line tool used to compress, optimize, and debug JavaScript code."],["This guide provides a basic example of using the Closure Compiler to minify a simple \"Hello World\" JavaScript function."],["The compiler removes unnecessary elements like comments and whitespace, shortens variable names, and produces a smaller, more efficient JavaScript file."],["You can confirm the functionality of the compiled code by including it in an HTML file and loading it in a browser."]]],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4