zhanbo_kz
zhanbo_kz

Reputation: 85

How to load single clojure file with dependencies using Clojure REPL (La Clojure)?

I am using La Clojure plugin for IntelliJ IDEA to load my clj file, then to retrieve the function inside that file. My Test.clj file has the following content:

(ns test.Test
  (:require [clojure.tools.logging :as logger])
 )

 (defn addx [a b c]
   (logger/debug "adding...")
   (+ a b c))

On Clojure REPL console I am trying to load the file by:

(load-file "/home/.../test/Test.clj")

That gives me the next error:

FileNotFoundException Could not locate clojure/tools/logging__init.class or clojure/tools/logging.clj on classpath: clojure.lang.RT.load (RT.java:443)

Could someone tell me what I am doing wrong?

Thank you.

Upvotes: 3

Views: 871

Answers (1)

Tim
Tim

Reputation: 13058

What leads to the error is that classpath (the one used to start Clojure REPL) doesn't include the clojure.tools.logging library. Test.clj requires it, so REPL, upon loading it, fails to find the required package, hence the error.

I'm not sure how you start the REPL, but the rest of the answer assumes that this is a regular REPL in a terminal (not from the IntelliJ IDEA itself; if the REPL is started from IntelliJ IDEA - I would say it is the misconfiguration of that last one, because it is supposed to start REPL with the correct project classpath - first thing to check would be the project configuration).

So to fix that, ensure that REPL has all dependencies (in this case - clojure.tools.logging) in the classpath. For example, probably the simplest thing one can do is to add a project.clj with dependencies declared in it, and use Lieningen to start the REPL. Bare bones project.clj which would do the trick is:

(defproject my-project "0.0.1-SNAPSHOT"
  :dependencies [[org.clojure/tools.logging "0.3.1"]])

Now, from the same directory where project.clj is in:

$ lein repl 
...
user=> (load-file "Test.clj")
#'test.Test/addx
user=> (test.Test/addx 13 9 20)
42

Or, if you want to go "pure Clojure REPL no Leiningen", you should have dependencies jar files locally (for example, download from Maven Central), and then you can add them the classpath when starting up the REPL:

$ java -cp /path/to/clojure.jar:/path/to/tools.logging-0.3.1.jar clojure.main

and it should normally give the same result.

Upvotes: 2

Related Questions