11

I'm new to Clojure, and I don't quite understand how to write my project.clj so it works for both lein repl and lein run. Here it is (whole path: ~/my-project/project.clj):

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

Then I have my ~/my-project/src/my_project/core.clj file

(ns my-project.core)

(defn hello []
  (println "Hello world!")
)

lein run works just fine but I get a FileNotFoundException when running lein repl:

~/my-project$ lein run
Hello world!
~/my-project$ lein repl
REPL started; server listening on localhost port 42144
FileNotFoundException Could not locate hello__init.class or hello.clj on classpath:   clojure.lang.RT.load (RT.java:430)
clojure.core=>

How should I edit the project.clj to solve this? Or do I have to call lein repl in a different way?

Thanks in advance.

EDIT: tried with lein deps and lein compile, but still the same error

~/my-project$ lein version
Leiningen 1.7.1 on Java 1.6.0_27 OpenJDK Client VM
~/my-project$ lein deps
Copying 1 file to /home/yasin/Programming/Clojure/my-project/lib
~/my-project$ lein compile
No namespaces to :aot compile listed in project.clj.
~/my-project$ lein repl
REPL started; server listening on localhost port 41945
FileNotFoundException Could not locate hello__init.class or hello.clj on classpath:   clojure.lang.RT.load (RT.java:430)
4

1 回答 1

18

你可以做的一件事是让它工作core.clj

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

(defn hello []
  (println "Hello world!"))

(defn -main []
  (hello))

并将其编辑project.clj为:

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

(:gen-class)告诉编译器为命名空间生成一个 Java 类,并且:mainin 中的指令project.clj将告诉lein run在该类上运行 main 方法,该方法由-main. 我不清楚为什么lein repl找不到my-project.core/hello,但我对 leiningen 内部结构知之甚少。

于 2013-03-11T13:40:38.300 回答