Hello World in Clojure

Our first program will print the classic “hello world” message. Here’s the full source code.

(ns hello-world.core)

(defn -main []
  (println "hello world"))

To run the program, put the code in a file named hello_world/core.clj and use the clojure command.

$ clojure -m hello-world.core
hello world

Sometimes we’ll want to build our programs into binaries. In Clojure, this usually means using a tool like Leiningen to create an uberjar, a standalone JAR file.

First, initialize a Leiningen project:

$ lein new app hello-world

Then, place the code inside src/hello_world/core.clj and build the uberjar:

$ lein uberjar
$ ls
target/uberjar/hello-world-0.1.0-SNAPSHOT-standalone.jar

We can then execute the JAR file directly using the java -jar command.

$ java -jar target/uberjar/hello-world-0.1.0-SNAPSHOT-standalone.jar
hello world

Now that we can run and build basic Clojure programs, let’s learn more about the language.