views:

140

answers:

3

I'm playing around with Clojure, and I can't figure out how to import a function from clojure-contrib.jar. Working from this answer, I'm doing the following:

Running the REPL like so:

 java -cp clojure.jar:clojure-contrib.jar clojure.main

Then trying to import a function:

user=>  (use '[clojure-contrib.duck-streams :only (writer reader)])

It doesn't work, and I get the following error:

java.io.FileNotFoundException: Could not locate clojure_contrib/duck_streams__init.class or clojure_contrib/duck_streams.clj on classpath: (NO_SOURCE_FILE:0)

Trying it with a dot instead of a dash also doesn't work:

user=>  (use '[clojure.contrib.duck-streams :only (writer reader)])

I get mostly the same error:

java.io.FileNotFoundException: Could not locate clojure/contrib/duck_streams__init.class or clojure/contrib/duck_streams.clj on classpath: (NO_SOURCE_FILE:0)

What am I doing wrong?

+1  A: 

Is clojure.jar and clojure-contrib.jar in your current working directory? If not, you need to specify the full path to the JAR files in the CLASSPATH.

mipadi
When I tested this exact example, yes they were in my working directory. Specifying their full path doesn't change the error I get though.
Steve Armstrong
Adding the full path "/usr/lib/clojure-contrib-1.1.0/clojure-contrib.jar" to my CLASSPATH did it for me. It is the sole thing on my CLASSPATH; not that that should matter. I thought jar files would get picked up automatically, I guess I was wrong: http://en.wikipedia.org/wiki/Classpath_%28Java%29#Setting_the_path_of_a_Jar_file.
caseyboardman
A: 

It's clojure.contrib, not clojute-contrib. Note dot versus dash.

kotarak
Tried that, still didn't work. Updated question to include that test.
Steve Armstrong
Are you on Windows? Then you have to use `java -cp clojure.jar;clojure-contrib.jar clojure.main` with semicolon instead of colon.
kotarak
im running latest ubuntu
vick
i cant believe its so hard to run a simple clojure with neccessary jars on ubuntu
vick
A: 

This should work

(use 'clojure.contrib)

I don't have clojure handy right now to check, but

(use 'clojure.contrib :only (writer reader))

should also work

Dev er dev
I get the following error: java.io.FileNotFoundException: Could not locate clojure/contrib__init.class or clojure/contrib.clj on classpath: (NO_SOURCE_FILE:0)
Steve Armstrong
I redownloaded clojure-contrib.jar, and it works now. I had a corrupt or unusable jar.
Steve Armstrong
This answer is plain wrong. There is no clojure.contrib namespace, so `(use 'clojure.contrib)` cannot possibly do anything useful.
kotarak