2
votes

I setup emacs slime/clojure as written here.

When I run (doseq [p (.getURLs (java.lang.ClassLoader/getSystemClassLoader))] (println (.getPath p))) to get classpath, I get the following.

/Users/smcho/.swank-clojure/clojure-1.1.0-master-20091202.150145-1.jar
/Users/smcho/.swank-clojure/clojure-contrib-1.1.0-master-20091212.205045-1.jar
/Users/smcho/.swank-clojure/swank-clojure-1.1.0.jar

How can I add classpath for emacs/slime for clojure?

3

3 Answers

3
votes

The procedure differs depending on whether you are using slime-connect to start slime (by connect to a remote swank server, created with lein swank, for example) or you are starting slime using M-X slime.

If you're using slime-connect, you'll need to modify the classpath of the java process that is running the swank server. If you're starting the swank server using lein swank, simply add the jars you want to be part of your classpath to the project's lib directory.

On the other hand, if you're starting slime using M-X slime, the following elisp code will do the magic for you (just place it in your ~/.emacs file).

(eval-after-load "swank-clojure"
  '(progn
     (add-to-list 'swank-clojure-classpath
          "/Users/smcho/.clojure/")
     (add-to-list 'swank-clojure-classpath
          "/Users/smcho/.clojure/blah.jar")))

This will add /Users/smcho/.clojure/ and /Users/smcho/.clojure/blah.jar to the classpath. (Please note that you'll either need to restart emacs or reload the .emacs file: type M-X load-library and then type .emacs on the next prompt.)

2
votes

I believe the advised way to fire up a clojure reple nowadays is to use lein swank, and use \M-x slime-connect. See http://github.com/technomancy/leiningen/ for a detailed description of this tool.

Using leiningen, you can configure your project using a configuration file, project.clj. On this file you may require remotely (maven-)published jars, and by running lein deps you will get a "lib" directory with all the jars. Since the lein swank command uses that directory as a classpath, all you have to do is to add your jars to that directory.

That said, if you're still using \M-x swank-clojure-project, it will also detect your jars under that directory.

However, if you're just using a plain \M-x slime to fire a clojure repl, then I think there's no "clean" solution (aside from adding the path to your global environment's $CLASSPATH or to use some elisp voodoo - like the one in seh's answer - to change the java vm command arguments. But I believe this is only intended to do some quite basic experiments, and shouldn't be used for any project-based work (exactly for this reason!)

1
votes

If you're willing to hard-code the list of Jar files, this Emacs Lisp form should suffice:

(let ((base-path "/Users/smcho/.swank-clojure"))
  (setq swank-clojure-classpath
        (append
         swank-clojure-classpath
         (mapcar (lambda (d)
                   (expand-file-name d base-path))
                 '("clojure-1.1.0-master-20091202.150145-1.jar"
                   "clojure-contrib-1.1.0-master-20091212.205045-1.jar"
                   "swank-clojure/swank-clojure-1.1.0.jar")))))

That takes your list of Jar files and adds them to the variable swank-clojure-classpath. Note that this form must be evaluated by Emacs, not SLIME's Swank. It's used by Emacs to start up the Java process that will run Clojure and Swank within it.

There are more elaborate ways to set up the class path for a project, such as including Maven-style paths below a designated project root directory.