Tuesday, May 26, 2009

Clojure Classpath Made Easy

Java 6 has an improvement that makes it easy to add new jars to your Clojure classpath when starting the REPL. The -cp option now takes a wildcard, enabling you specify an entire directory of jars.

Here's what I do:

First, following the Groovy model, I create a .clojure subdirectory in my home directory.

Next, I copy clojure-1.0.0.jar and jline-0.9.94.jar to the .clojure directory.

Finally, I create a bash script named clj, making sure it is in my PATH:

java -cp $HOME/.clojure/*: jline.ConsoleRunner clojure.lang.Repl $1

Be sure to terminate the wildcard with a path separator or it won't work. Whenever you need a new jar in your classpath (e.g., a database driver like Oracle's ojdbc14.jar), simply drop the jar in your .clojure directory and your next invocation of the clj script will pick up the new jar.

1 comment:

MCAndre said...

Clojure can't find and compile scripts in the current directory.

$ cat hello.clj
#!/usr/bin/env clj

(ns hello

(defn -main
(println (str "Hello " greetee "!")))wonko:Desktop andrew$
wonko:Desktop andrew$ clj
Clojure 1.3.0-master-SNAPSHOT
user=> (compile 'hello)
CompilerException java.io.IOException: No such file or directory, compiling:(hello.clj:3)