Lesson 17. Running a program from the command line
Almost all the pieces are now in place to allow you to write a Clojure program that runs from the command line. You already know how write Clojure code that:
- Converts strings into data
- Does some calculation with the data
- Prints the results to the console
The only piece that is missing is how to organize your code in files and how to write and execute a command line that runs a Clojure program.
After reading this lesson, you will be able to:
- Organize Clojure code in namespaces and files
- Write an entry point for a program
- Run a Clojure program from the command line
In this section, we are going to write the simplest program you can think of: a program that runs from the command line without any arguments and displays "hello" to the console.
When we run a Clojure program from the command line, we specify the namespace where the entry point of the program is located. Currently, we will use namespaces mechanically. Namespaces will be covered in details in 11. The main thing to know about namespaces is that there is a one-to-one correspondence between namespaces and source files. Clojure files have the .clj suffix. For instance, the namespace corresponding to a file named hello.clj is hello.
Now, if the entry point of our program is in a namespace named hello, we run the program with this command line:
$ clojure -m hello
Usually, the source files of a Clojure program are stored on a folder named src.