11

I have installed the libraries with Maven to the ~/.m2/repository/ directory. I would like to add that path to the default Clojure classpath. I could not find the documentation how to do that.

Any hints?

Cheers!

clj
Clojure 1.4.0
user=> (require '[clojure.java.jmx :as jmx])
FileNotFoundException Could not locate clojure/java/jmx__init.class or clojure/java/jmx.clj on classpath:   clojure.lang.RT.load (RT.java:432)

The class path by default is:

user=> (println (seq (.getURLs (java.lang.ClassLoader/getSystemClassLoader))))
(#<URL file:/Users/myuser/cljmx/> #<URL file:/usr/local/Cellar/clojure/1.4.0/clojure-1.4.0.jar> #<URL file:/Users/myuser/cljmx/>)
nil
Istvan
  • 6,372
  • 7
  • 43
  • 81

4 Answers4

12

Leiningen really makes this process a lot less painful by keeping the setting of the classpath associated with the project, and more importantly leads to a repeatable build process. where you can come back to the project years later and still get a repl. A general outline of using leiningen in these cases:

  • lein new projectname
  • add the library you need to your project.clj file with a name you choose
  • run lein deps to print out the command to use to add the jar to your local repo
  • add the jar
  • run lein deps again (you can skip this step if using leiningen2)
  • run lein repl
  • enjoy

this is assuming that the library you are using is not already part of or available from a package in a maven repo, which many are.

TacticalCoder
  • 6,116
  • 3
  • 26
  • 37
Arthur Ulfeldt
  • 87,736
  • 24
  • 197
  • 278
10

The non-painful, popular method is to not mess with maven and classpaths and the JRE directly and use leiningen: https://github.com/technomancy/leiningen/

Otherwise, you can modify whatever is in clj and add/set the classpath in whatever ways java likes. See for example Setting multiple jars in java classpath

Community
  • 1
  • 1
Joost Diepenmaat
  • 17,218
  • 3
  • 41
  • 51
10

It should be noted that you also have the option of adding classpaths at runtime with the library pomegranate https://github.com/cemerick/pomegranate

This lets you do like:

 (require '[cemerick.pomegranate :as pom])
 (pom/add-classpath "/home/user/~.m2/....")
Tucker
  • 6,307
  • 6
  • 34
  • 55
bmillare
  • 4,143
  • 1
  • 21
  • 42
7

I assume that clj is a script to start Clojure REPL. Take a look into this script and find line similar to this:

java -cp /path/to/clojure.jar clojure.main

Here you start class clojure.main having "clojure.jar" on your classpath. To add more jars just add them to the end of -cp option values. E.g. on Linux:

java -cp /path/to/clojure.jar:/path/to/mylib.jar clojure.main

(use ; instead of : on Windows)

However, very soon you'll get tired of this way and will look for project management tool. So it makes sense to start using it right now. Take a look at Leiningen - it manages dependencies for you based on Maven (so it will be extremely easy to add new jar) and has REPL.

ffriend
  • 24,834
  • 13
  • 81
  • 125
  • Using java 6 and beyond /path/to/mylib.jar can be changed to /path/to/jarfolder/* and it will include all *.jar files in the classpath – GregA100k Aug 19 '13 at 22:03