How do I require and provide clojure files?

This one solved my problem and I have looked through countless other issues here so I would like to clarify.

The easiest way in emacs (on linux) is to do something like this:

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

(note the "lib/*":. given to -cp

Then you can use M-x slime-connect to connect with this instance.

Don't know if it's required, but I have read that it's a good idea to use the same version of clojure, clojure-contrib and swank-clojure on both sides.

You can also setup the path inside emacs by consing the "." to swank-clojure-classpath.


Besides "load"ing source files, you can also use the leiningen "checkout dependencies" feature. If you have two leiningen projects, say, project A requires B (provider). In the root directory of project A, create a directory called "checkouts". Inside "/checkouts" make a symbolic link to the root directory of project B.

- project A
  - project.clj
  - checkouts
    - symlink to project B
  - src
  - test

in project A's project.clj, declare project B as a dependency in the :dependencies section as if it were a project in clojars.org. E.g.

(defproject project-a
:dependencies [[org.clojure/clojure "1.5.1"]
               [project-b "0.1.0"]])

The thing though is, you have to go into project B and type:

lein install

That will compile project B's files into a jar file, which will appear in your ~/.m2 directory, which is kind of like your local clojars.org cache.

Once you set all this up, in your *.clj src file(s) of project A, you can "require" project B files in the normal way, as if it were from clojars.org:

(ns project-a.core
  (:require [project-b.core :as pb])

This way, you can refer to functions in your project-b.core the usual way like this:

pb/myfunction1

In my opinion, this is a pretty good way to share libraries and data between Leiningen projects while trying keep each Leiningen project as independent, self-contained, and minimal as possible.


You have a few options.

If it’s just a file (not in a package) then in your files, you can just use load. If your file was named “fun.clj”, you would just use the name of the file without the extension:

 (load "fun")

(provided fun.clj was on your classpath). Or

 (load "files/fun") 

if it wasn’t on your classpath but in the files directory.

Or you could use load-file and pass it the location of your file:

(load-file "./files/fun.clj")

If you wanted to namespace them (put them in a package), then you’d put the ns macro at the beginning of your file, again put it on your classpath. Then you could load it via use or require.

Here are the docs for the functions I’ve described:

  • load
  • load-file
  • ns
  • use
  • require