Learn how to write and run a simple 'Hello, World!' application in Clojure using Leiningen and the Clojure CLI. This guide is tailored for Java developers transitioning to Clojure.
Welcome to your first step in Clojure programming! In this section, we will guide you through writing a simple “Hello, World!” application using Clojure. This exercise will help you get comfortable with the basic setup and execution of a Clojure program, leveraging your existing Java knowledge to ease the transition.
Before diving into the code, let’s briefly discuss the tools we’ll use:
clj
): A command-line tool for running Clojure code, offering a straightforward way to execute scripts and manage dependencies.Leiningen is a popular choice for managing Clojure projects. Let’s create a simple project and run a “Hello, World!” application.
Open your terminal and execute the following command to create a new Leiningen project:
lein new app hello-world
This command creates a new directory named hello-world
with a basic project structure. Let’s explore the structure:
project.clj
: The project configuration file, similar to pom.xml
in Maven.src/hello_world/core.clj
: The main source file where we’ll write our code.test/hello_world/core_test.clj
: A placeholder for unit tests.Navigate to the src/hello_world/core.clj
file and open it in your favorite editor. Replace its contents with the following code:
(ns hello-world.core)
(defn -main
"A simple function that prints 'Hello, World!' to the console."
[]
(println "Hello, World!"))
Explanation:
ns
: Declares a namespace, similar to a package in Java.defn
: Defines a function. Here, -main
is the entry point of our application.println
: A built-in function to print text to the console.To execute the program, navigate to the project directory in your terminal and run:
lein run
You should see the output:
Hello, World!
The Clojure CLI offers a lightweight alternative to Leiningen for running Clojure code. Let’s see how to achieve the same result using clj
.
Create a new directory for your project:
mkdir hello-world-cli
cd hello-world-cli
Inside the hello-world-cli
directory, create a file named hello_world.clj
and add the following code:
(ns hello-world)
(defn -main
"A simple function that prints 'Hello, World!' to the console."
[]
(println "Hello, World!"))
deps.edn
File§Create a deps.edn
file in the same directory with the following content:
{:paths ["."]
:deps {}}
Explanation:
:paths
: Specifies directories to include in the classpath.:deps
: Lists dependencies, which are empty for this simple example.Execute the following command to run your program:
clj -M -m hello-world
The -M
flag specifies the main alias, and -m
indicates the namespace containing the -main
function. You should see:
Hello, World!
Let’s compare the Clojure “Hello, World!” with its Java counterpart:
Java Example:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Comparison:
main
function, but Clojure’s is defined with defn
.println
, similar to Java’s System.out.println
.Experiment with the following modifications to deepen your understanding:
println
to print a different message.-main
.Below is a simple flowchart illustrating the process of creating and running a Clojure “Hello, World!” application using Leiningen:
Diagram Explanation: This flowchart outlines the steps from project creation to execution, highlighting the simplicity and efficiency of using Leiningen.
project.clj
and use it in your program.Now that you’ve successfully written and executed your first Clojure program, you’re ready to explore more complex applications and leverage Clojure’s powerful features in your projects!