How to run .clj file as a script using leningen?

Hanfei Sun picture Hanfei Sun · Aug 22, 2012 · Viewed 15.5k times · Source

This is the second question after Is there a standalone Clojure package within Leiningen?

For example, I have a file hello_world.clj, and I can run it using

java -cp clojure.jar clojure.main hello_world.clj.

Since lein already contains Clojure (because I can run lein repl directly), is there a way to do the same thing like

lein script hello_world.clj by lein?

Answer

Ming picture Ming · Jan 29, 2015

There are several ways with lein repl:

  • Very slow: cat your_file.clj | lein repl
  • Slow: echo '(load-file "your_file.clj")' | lein repl
  • Fast:
    1. lein repl
    2. (load-file "your_file.clj")
    3. repeat 2