java 如何将目录添加到 Clojure 的类路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11973694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add directory to Clojure's classpath?
提问by Istvan
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.
我已经使用 Maven 将库安装到 ~/.m2/repository/ 目录。我想将该路径添加到默认的 Clojure 类路径。我找不到如何做到这一点的文档。
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
采纳答案by Joost Diepenmaat
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/
不痛苦的流行方法是不要直接弄乱 Maven 和类路径以及 JRE 并使用 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
否则,您可以修改其中的任何内容clj
并以 java 喜欢的任何方式添加/设置类路径。参见例如在 java 类路径中设置多个 jars
回答by Arthur Ulfeldt
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:
Leiningen 通过保持与项目相关联的类路径的设置,确实让这个过程减少了很多痛苦,更重要的是导致了一个可重复的构建过程。你可以在几年后回到这个项目并且仍然得到一个repl。在这些情况下使用 leiningen 的一般概述:
- 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
- lein 新项目名称
- 使用您选择的名称将您需要的库添加到您的 project.clj 文件中
- 运行 lein deps 打印出用于将 jar 添加到本地存储库的命令
- 添加罐子
- 再次运行 lein deps(如果使用 leiningen2 可以跳过这一步)
- 运行 lein repl
- 请享用
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.
这是假设您正在使用的库还不是 Maven 存储库中的包的一部分或可从该包中获得,其中许多是。
回答by bmillare
It should be noted that you also have the option of adding classpaths at runtime with the library pomegranate https://github.com/cemerick/pomegranate
应该注意的是,您还可以选择在运行时使用库 pomegranate 添加类路径https://github.com/cemerick/pomegranate
This lets you do like:
这让您可以这样做:
(require '[cemerick.pomegranate :as pom])
(pom/add-classpath "/home/user/~.m2/....")
回答by ffriend
I assume that clj
is a script to start Clojure REPL. Take a look into this script and find line similar to this:
我假设这clj
是一个启动 Clojure REPL 的脚本。查看此脚本并找到与此类似的行:
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:
在这里,您开始上课clojure.main
,在您的类路径上有“clojure.jar”。要添加更多 jar,只需将它们添加到-cp
选项值的末尾。例如在 Linux 上:
java -cp /path/to/clojure.jar:/path/to/mylib.jar clojure.main
(use ;
instead of :
on Windows)
(使用;
而不是:
在 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.
但是,很快您就会厌倦这种方式,并会寻找项目管理工具。所以现在开始使用它是有意义的。看看Leiningen- 它基于 Maven 为您管理依赖项(因此添加新 jar 将非常容易)并且具有 REPL。