3
votes

For the past 6 months, I have been running my Clojure app from the repl.

I.e., whenever I want to run the app, I load up a clojure repl, then I type in: (load-file "src/run.clj") ... and it runs my app.

Then, clojure loads up all of my *.clj files, compiles them, and runs.

I have recently switched over to Lein. I would like to "compile ahead of time / package up" my code, so that I can get a single jar, with Main method, and run it directly on the command line via "java ..." rather than having to load up a clojure/lein repl.

It's clear to me that I need to do some RTFMing. However, if someone could point me at a good tutorial, it would be much appreciated.

Thanks!

1

1 Answers

5
votes

There is also a nice tutorial in the official lein tutorial. I'm just including this information in case we end up with any dead links in the future.

1) You first need to designate the main namespace by adding :main to project.clj

(defproject yourproject "0.1"
   :dependencies [[org.clojure/clojure "1.4.0"]]
   :main yourproject.core)

2) In your designated main namespace you must add (:gen-class) and you must designate the main function by using (defn -main ...)

(ns yourproject.core
  (:gen-class))

(defn -main [& args]
  (println "This is your crazy project!"))

3) run uberjar to create the standalone jar

lein uberjar

4) run your program with java -jar

java -jar yourproject.jar