Question

I've just installed criterium, and I want to be able to use it from the repl and from other projects. How do I extend the java classpath so that I can use the installed library from anywhere?

I'd appreciate answers that can work easily on all projects I work on in the future. I'm using leiningen, and I would suppose that it would do this when you run lein install, but either I'm doing something wrong or that isn't enough.

Was it helpful?

Solution

you have to learn basic maven (lein uses maven under the hood) and java classpath mechanics. For a very brief overview:

maven stores dependencies in a hidden directory, .m2, in your home folder. Browse around the folder and it will make sense. When you run lein install it will put the jar in that folder. Try to find the one you installed. However, most dependencies you use will be on a server like clojars so you shouldn't need to manually install it into like you did. for example, criterium is here.

In your project you need to add criterium as a dependency in project.clj. It will look something like this:

(defproject your-project "0.1.5" :description "blah" 
  :dependencies [[org.clojure/clojure "1.2.0"]
                 [org.clojure/clojure-contrib "1.2.0"]
                 [criterium "0.0.1-SNAPSHOT"]
                 ]   
  :dev-dependencies
    [[swank-clojure "1.2.1"]])

Now cd into the root folder of your project and run lein deps. This will download the dependencies in project.clj, and automatically put them in .m2 and into the lib folder of your project. To start a repl with the classpath setup run lein repl

Each project that uses criterium will need to add it as a dependency in it's project.clj

OTHER TIPS

I'd appreciate answers that can work easily on all projects I work on in the future.

Unfortunately this is impossible on the JVM; you can't modify the classpath at runtime, so it must be calculated separately for each project. There is no system-wide classpath, though tools like Leiningen and cljr may provide something similar in some contexts:

$ lein install swank-clojure 1.3.0-SNAPSHOT

This will install swank-clojure into ~/.lein/plugins, which will make it available on all Leiningen projects as a dev-dependency and will make it available when you do "lein repl" outside the context of a project, but things that are actual dependencies of projects will need to go in that project's project.clj file. Otherwise it would be easy to create projects that work on your machine but are not repeatable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top