Clojure character rating error

So, I have this code:

(ns contact-form.core
  (:gen-class))

(def foo "Hello World!")

(defn some-func [a-symbol]
  (println (str a-symbol " value is: " (eval a-symbol))))

(defn -main [& args]
  (some-func 'foo))

After running C-c C-kin Emacs, I get the following output:

contact-form.core> (-main)
foo value is: Hello World!
nil

But when I do lein uberjarand run the resulting jar file, I get an error message:

Exception in thread "main" java.lang.RuntimeException: Unable to resolve symbol: foo in this context, compiling:(NO_SOURCE_PATH:0)
    at clojure.lang.Compiler.analyze(Compiler.java:6235)
    at clojure.lang.Compiler.analyze(Compiler.java:6177)
    at clojure.lang.Compiler.eval(Compiler.java:6469)
    at clojure.lang.Compiler.eval(Compiler.java:6431)
    at clojure.core$eval.invoke(core.clj:2795)
    at contact_form.core$some_func.invoke(core.clj:7)
    at contact_form.core$_main.doInvoke(core.clj:10)
    at clojure.lang.RestFn.invoke(RestFn.java:397)
    at clojure.lang.AFn.applyToHelper(AFn.java:159)
    at clojure.lang.RestFn.applyTo(RestFn.java:132)
    at contact_form.core.main(Unknown Source)
Caused by: java.lang.RuntimeException: Unable to resolve symbol: foo in this context
    at clojure.lang.Util.runtimeException(Util.java:156)
    at clojure.lang.Compiler.resolveIn(Compiler.java:6720)
    at clojure.lang.Compiler.resolve(Compiler.java:6664)
    at clojure.lang.Compiler.analyzeSymbol(Compiler.java:6625)
    at clojure.lang.Compiler.analyze(Compiler.java:6198)
    ... 10 more

I have two questions:

  • Why does uberjar not work just like REPL?
  • What can I do to fix this problem?
+5
source share
1 answer

1. Why does uberjar work differently with REPL?

The reason for the "NO_SOURCE_PATH" error is that you are not in the namespace that defined "foo".

: REPL , contact-form.core, , (ns contact-form.core) REPL, user -main :

contact-form.core=> (-main)
foo value is: Hello World!
nil
contact-form.core=> (ns user)
nil
user=> (contact-form.core/-main)
CompilerException java.lang.RuntimeException: Unable to resolve symbol: foo in this context, compiling:(NO_SOURCE_PATH:120) 
user=> 

, main uberjar ( REPL), (contact-form.core/-main) , clojure.core, (ns contact-form.core) . : main ( ) , contact-form.core .

2.

.:

(defn -main [& args]
  (use 'contact-form.core)
  (some-func 'foo))
+5

All Articles