在尝试使用外部库时,我在Clojure REPL中收到FileNotFoundException
。例如,我使用lein new example-twitter-project
创建了一个新项目。然后我编辑文件:
project.clj
:
(defproject example-twitter-project "1.0.0-SNAPSHOT"
:description "FIXME: write description"
:dependencies [[org.clojure/clojure "1.3.0"]
[clojure-twitter "1.2.6-SNAPSHOT"]])
src/example-twitter-project/core.clj
:
(ns example-twitter-project.core
(:use 'twitter))
然后我运行lein deps
,然后lein repl
。
example-twitter-project$ lein repl
REPL started; server listening on localhost port 23833
user=> (use :reload-all 'example-twitter-project.core)
FileNotFoundException Could not locate quote/twitter__init.class or quote/twitter.clj on classpath: clojure.lang.RT.load (RT.java:430)
同时,我可以直接从REPL use
user=> (use 'twitter)
nil
user=> (doc twitter/with-oauth)
-------------------------
twitter/with-oauth
([consumer access-token access-token-secret & body])
Macro
Set the OAuth access token to be used for all contained Twitter requests.
nil
外部库:
-main
如何才能在项目中使用此外部库?
如果我添加FileNotFoundException
函数并尝试运行脚本,我会得到类似的Exception in thread "main" java.lang.RuntimeException: java.io.FileNotFoundException: Could not locate quote/twitter__init.class or quote/twitter.clj on classpath:
at clojure.lang.Util.runtimeException(Util.java:165)
...
Caused by: java.io.FileNotFoundException: Could not locate quote/twitter__init.class or quote/twitter.clj on classpath:
at clojure.lang.RT.load(RT.java:430)
...
,因此这不仅是REPL问题。
lein version
请注意,库的名称及其名称空间不同。这可能是原因吗?
P.S。 {{1}}:Java 1.6.0_24 OpenJDK 64位服务器虚拟机上的Leiningen 1.7.1
答案 0 :(得分:5)
在ns
声明中你不需要引用ns名称,在use
表达式中你必须引用它们
(ns example-twitter-project.core
(:use twitter))
这是因为ns宏在评估之前会看到它的参数,它会看到符号twitter
,而不是查找twitter的值。从REPL调用use
时,将在use
查看之前评估符号twitter,除非您使用引号来阻止此操作。
答案 1 :(得分:3)
您无需在quote
中使用(ns (:use ...))
,因此您的代码将如下所示:
(ns example-twitter-project.core
(:use twitter))