5

I have downloaded and installed a jar with Maven (as answered here) but now I need to require it inside of a project. I followed the instructions on the git README ((require '[clj-http.client :as client])) but I still get this error:

FileNotFoundException Could not locate clj_http/client__init.class or clj_http/client.clj on classpath:   clojure.lang.RT.load (RT.java:430)

3 Answers 3

5

The other answers on this thread will certainly work .... But adding jars directly on your machine's CLASSPATH or at the command line can be a very difficult strategy for development . . .

The most common, idiomatic way to include jars in a clojure app is Leiningen (easy, one step install on github -- begginers should check note at the end of this answer for a caveat)... Leiningen can also install the clojure environment and launch your repl for you, preloaded with the right jar environment.

It is essentially a java dependency manager and build tool rolled into one - i.e. like ivy or the maven Pom.xml which we use for java development.

A Few examples of how to use Leiningen to interact with multiple libs in a simple and scalable fashion :

To launch a repl, such that the jars in your project.clj file are on the classpath :

lein repl

To update your jars in your maven repo specified by your project.clj :

lein deps

Finally , lein let's you export "uberjars" which are akin to "fatjars", i.e. they have all the dependencies bundled for you.

A minor update regarding the new Lein version : Note for begginers.

There are two scripts you can run to install Leiningan, it might be safer to run this one:

https://github.com/jayunit100/leiningen/blob/stable/bin/lein (the stable release)

Rather than this one:

https://raw.github.com/technomancy/leiningen/preview/bin/lein (the latest update, which is a preview).

1
  • This is more of a content change than I'm willing to make to someone else's post without discussion -- but IMHO, the bolding really does a lot to reduce readability here. Getting rid of the first paragraph and debolding the second one would probably make for a cleaner answer. May 30, 2012 at 19:24
5

when you start the java process you need to do:

java -cp ./lib/clj-http.jar ./lib/clojure.jar clojure.main

Basically you need both clojure.jar (which is where the REPL is at) AND clj-http.jar in the classpath.

Assuming clj-http.jar is in ./lib/ directory. Or you could build your project with lein (build tool, similar to maven) and have it build an uberjar, which is what I would do. There's a pretty good walkthrough of setting up lein and building an uberjar here: http://zef.me/2470/building-clojure-projects-with-leiningen.

9
  • So I have to build an uberjar whenever I add dependencies? I created a folder at ~/.clojure where I will just dump all of my jars. I tried java -cp ~/.clojure/clj-http-0.4.1.jar clojure.main but no luck. It doesn't like the last part
    – Chris
    May 30, 2012 at 14:47
  • 1
    Chris: my clojure bash script for you ref: github.com/number23/iLibrary/blob/master/clojure/clojure May 30, 2012 at 14:51
  • So I have to add things to the class path every time I want to run the REPL? That sounds... less than ideal
    – Chris
    May 30, 2012 at 14:55
  • 1
    I think if you use lein, and run lein repl it will run the repl with any dependencies set in your project.clj file. Or you could write your own launcher script that adds anything in ./lib to the classpath of the started java process. But ultimately it's just java. So yes, you do need to set dependencies in the classpath somehow.
    – Kevin
    May 30, 2012 at 14:59
  • 1
    Another option is setting the CLASSPATH environment variable, but again you'd need to do this each time you opened a new shell.
    – Kevin
    May 30, 2012 at 15:01
1

add clj-http.jar to CLASSPATH or CP before you start clojure repl.

1
  • I looked up add-classpath and it has been deprecated. How would I add the jar to the class path?
    – Chris
    May 30, 2012 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.