Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

including jars in clojure

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)
like image 522
Chris Avatar asked May 30 '12 14:05

Chris


3 Answers

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).

like image 62
jayunit100 Avatar answered Nov 03 '22 11:11

jayunit100


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.

like image 45
Kevin Avatar answered Nov 03 '22 11:11

Kevin


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

like image 1
number23_cn Avatar answered Nov 03 '22 13:11

number23_cn