Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define project.clj for both lein run and lein repl to work?

Tags:

clojure

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)
like image 253
m0skit0 Avatar asked Mar 11 '13 12:03

m0skit0


People also ask

How do I run a Clojure project?

Create a new Clojure project with Leiningen. Build the project to create an executable JAR file. Execute the JAR file. Execute code in a Clojure REPL.

What is Lein DEPS?

The goal of the "deps" target is to ensure that every dependency required to run this project is available in your local maven repo. In short it populates ~/.m2/... with jars that need to be on the class-path for the project to run. If lein finds the dependency, which is the required version of a project, in ~/.

How do I start Clojure REPL?

To start a REPL session in Eclipse, click the Menu option, go to Run As → Clojure Application. This will start a new REPL session in a separate window along with the console output. Conceptually, REPL is similar to Secure Shell (SSH).


1 Answers

One thing you could do to get it to work would be to change core.clj to:

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

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

(defn -main []
  (hello))

And edit the project.clj to:

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

The (:gen-class) will tell the compiler to generate a Java class for the namespace, and the :main directive in project.clj will tell lein run to run the main method on the class, which is given by -main. Why lein repl was failing to find my-project.core/hello is unclear to me, but I don't know much about leiningen internals.

like image 96
ToBeReplaced Avatar answered Jan 22 '23 02:01

ToBeReplaced