9
votes

I'm trying to add paths to my classpath in the Clojure REPL that I've set up in Emacs using ELPA. Apparently, this isn't the $CLASSPATH environment variable, but rather the swank-clojure-classpath variable that Swank sets up. Because I used ELPA to install Swank, Clojure, etc., there are a ton of .el files that take care of everything instead of my .emacs file. Unfortunately, I can't figure out how to change the classpath now.

I've tried using (setq 'swank-clojure-extra-classpaths (list ...)) both before and after the ELPA stuff in my .emacs, and I've tried adding paths directly to swank-clojure-classpath in .emacs, .emacs.d/init.el, and .emacs.d/user/user.el, but nothing works.

What I'm ultimately trying to do is to add both the current directory "." and the directory in which I keep my Clojure programs. I'm assuming swank-clojure-classpath is the thing I need to set here. Thanks for your help.

3

3 Answers

10
votes

As mac says, you can use

M-x swank-clojure-project

to establish a slime REPL to a clojure project; the command will ask you for your projects root directory, and will establish a classpath that includes a variety of directories including src/ lib/ and resources/ if they are present.

Alternatively, if you are using leiningen, you can start that in a terminal with the command

$ lein swank

from inside your project root directory. This will establish a standard project classpath (as above). From here you can connect to this running process via Emacs with the command

M-x slime-connect

Finally a third option which I'd recommend is to connect via Emacs/slime (with M-x slime-connect) to a process started by your own shell script which specifies a custom set of JVM command line arguments e.g.

#!/bin/bash 

java -server -cp "./lib/*":./src clojure.main -e "(do (require 'swank.swank) (swank.swank/start-repl))"

This allows you explicit control over how the VM is started, and is likely similar to what you will likely have to do in production anyway.

3
votes

You want:

M-x swank-clojure-project

This adds all jars in your /lib dir.

If you want to :use a clojure file (bar.clj), in for instance /foo you would do:

(ns foo
  (:use foo.bar)) 
2
votes

Sorry I cannot help you with an answer, but maybe your question is wrong:

I myself haven't started a clojure session from within Emacs for ages. I think the better way is to describe all your dependencies in a single place (e.g. the maven pom.xml or leiningen's project.clj) and then start a swank session with those dependencies. I.e. add the swank-clojure lib to your (dev-)dependencies and then use lein swank or maven swank (not sure about the last one -- haven't used that much and not in a while) from the command line to start a swank session and use M-x slime-connect to attach to that session.

The advantage is that you get all the things in your classpath that you need -- and not more, so you cannot mistakenly use something from the repl that your final project doesn't know about.

This blog post gives a good summary how to do this right.