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:

#!/bin/bash
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.