Clojure/Emacs/nrepl: Ctrl X + Ctrl E leads to 'FileNotFoundException Could not locate […] on classpath'
I've been playing around with Clojure using Emacs and nrepl recently and my normal work flow is to write some code in Emacs and then have it evaluated in nrepl by typing Ctrl X + Ctrl E at the end of the function.
I tried this once recently and got the following exception instead of a successful evaluation:
FileNotFoundException Could not locate ranking_algorithms/ranking__init.class or ranking_algorithms/ranking.clj on classpath: clojure.lang.RT.load (RT.java:432)
I was a bit surprised because I had nrepl running already (via (Meta + X) + Enter + nrepl-jack-in) and I'd only ever seen that exception refer to dependencies which weren't in my project.clj file at the time I launched nrepl.
I eventually came across this StackOverflow post which suggested that you either launch nrepl using leiningen and then connect to it from Emacs or have your project.clj open when running (Meta + X) + Enter + nrepl-jack-in.
To launch nrepl from leiningen we'd run the following command from the terminal:
$ lein repl nREPL server started on port 52265 REPL-y 0.1.0-beta10 Clojure 1.4.0 Exit: Control+D or (exit) or (quit) Commands: (user/help) Docs: (doc function-name-here) (find-doc "part-of-name-here") Source: (source function-name-here) (user/sourcery function-name-here) Javadoc: (javadoc java-object-or-class-here) Examples from clojuredocs.org: [clojuredocs or cdoc] (user/clojuredocs name-here) (user/clojuredocs "ns-here" "name-here")
We can then connect to that nrepl server from Emacs by typing (Meta + X) + Enter + nrepl which seems to work quite nicely.
To check the nrepl-jack-in approach works when we've got project.clj open we need to first kill the existing server by typing (Meta + X) + Enter + nrepl-quit.
Now if we type (Meta + X) + Enter + nrepl-jack-in our functions are evaluated correctly and all is well with the world again.