Question

I have recently started out with clojure, and I am using leiningen to create a small project. I am having troubles in getting leiningen to behave with command line arguments. The following is my src/project/core.clj

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

(defn -main [& args]
    (println (apply str args)))

and my project.clj

(defproject project "1.0.0-SNAPSHOT"
  :description "FIXME: write"
  :main project.core
  :dependencies [[org.clojure/clojure "1.2.0"]
                 [org.clojure/clojure-contrib "1.2.0"]])

Now when I run lein run arg1 arg2, it gives me this error:

Exception in thread "main" java.lang.Exception: Unable to resolve symbol: arg1 in this context (NO_SOURCE_FILE:1)
    at clojure.lang.Compiler.analyze(Compiler.java:5205)
    at clojure.lang.Compiler.analyze(Compiler.java:5151)
    at clojure.lang.Compiler$InvokeExpr.parse(Compiler.java:3057)
    at clojure.lang.Compiler.analyzeSeq(Compiler.java:5371)
    at clojure.lang.Compiler.analyze(Compiler.java:5190)
    at clojure.lang.Compiler.analyze(Compiler.java:5151)
    at clojure.lang.Compiler$BodyExpr$Parser.parse(Compiler.java:4670)
    at clojure.lang.Compiler$FnMethod.parse(Compiler.java:4328)
    at clojure.lang.Compiler$FnExpr.parse(Compiler.java:3173)
    at clojure.lang.Compiler.analyzeSeq(Compiler.java:5367)
    at clojure.lang.Compiler.analyze(Compiler.java:5190)
    at clojure.lang.Compiler.eval(Compiler.java:5421)
    at clojure.lang.Compiler.eval(Compiler.java:5415)
    at clojure.lang.Compiler.eval(Compiler.java:5391)
    at clojure.core$eval.invoke(core.clj:2382)
    at clojure.main$eval_opt.invoke(main.clj:235)
    at clojure.main$initialize.invoke(main.clj:254)
    at clojure.main$null_opt.invoke(main.clj:279)
    at clojure.main$main.doInvoke(main.clj:354)
    at clojure.lang.RestFn.invoke(RestFn.java:422)
    at clojure.lang.Var.invoke(Var.java:369)
    at clojure.lang.AFn.applyToHelper(AFn.java:165)
    at clojure.lang.Var.applyTo(Var.java:482)
    at clojure.main.main(main.java:37)
Caused by: java.lang.Exception: Unable to resolve symbol: arg1 in this context
    at clojure.lang.Compiler.resolveIn(Compiler.java:5677)
    at clojure.lang.Compiler.resolve(Compiler.java:5621)
    at clojure.lang.Compiler.analyzeSymbol(Compiler.java:5584)
    at clojure.lang.Compiler.analyze(Compiler.java:5172)
    ... 23 more

However, if I do a lein uberjar and then do java -jar project-1.0.0-SNAPSHOT-standalone.jar arg1 arg2, I get the correct output.

arg1arg2

It isn't very comfortable to have to create the uberjar to run it every time while development, is there a better way?

Was it helpful?

Solution

This looks like it's caused by a bug that's been fixed in git. The fix will be in 1.4.2, which should be out in a few days. In the mean time, you can use workarounds discussed here: http://groups.google.com/group/clojure/msg/a8160b23a5019a12

OTHER TIPS

From lein-run: "Args will be passed on as *command-line-args*"

So you will have to use those. The example on the site shows how. If you now what arguments you are passing you an also use :run-aliases to specify those in your project.clj. Again, the mentioned site has all the information.

My sample project.clj

    (defproject addressbook "1.0.0-SNAPSHOT"
  :description "FIXME: write"
  :main addressbook.core
  :run-aliases {:addressbook [addressbook.core -main "arg1"]}
  :dependencies [[org.clojure/clojure "1.2.0"]
                 [org.clojure/clojure-contrib "1.2.0"]]
  :dev-dependencies [[lein-run "1.0.0"]])

And the test code:

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

(defn -main [& [args]]
  (if args (println args)))

Both "lein run addressbook foo" as "lein uberjar" work for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top