How to add directory to Clojure's classpath?

北城以北 提交于 2019-12-21 03:58:49

问题


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

回答1:


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




回答2:


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.




回答3:


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



回答4:


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.



来源:https://stackoverflow.com/questions/11973694/how-to-add-directory-to-clojures-classpath

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!