sas*_*nin 2 clojure leiningen read-eval-print-loop
FileNotFoundException在尝试使用外部库时,我在Clojure REPL中收到.例如,我创建了一个新项目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"]])
Run Code Online (Sandbox Code Playgroud)
src/example-twitter-project/core.clj:
(ns example-twitter-project.core
(:use 'twitter))
Run Code Online (Sandbox Code Playgroud)
然后我跑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)
Run Code Online (Sandbox Code Playgroud)
同时,我可以use直接从REPL中获取外部库:
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
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能在项目中使用这个外部库?
如果我添加-main函数并尝试运行脚本,我会得到类似的FileNotFoundException,所以这不仅是一个REPL问题.
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)
...
Run Code Online (Sandbox Code Playgroud)
请注意,库的名称及其名称空间是不同的.这可能是原因吗?
PS lein version:Leiningen 1.7.1 on Java 1.6.0_24 OpenJDK 64位服务器VM
在ns声明中你不需要引用ns名称,在use表达式中你必须引用它们
(ns example-twitter-project.core
(:use twitter))
Run Code Online (Sandbox Code Playgroud)
这是因为ns宏在评估之前会看到它的参数,它会看到符号twitter而不是查找twitter的值.当use从REPL 调用时,将在use查看之前评估符号twitter ,除非您使用quote来防止这种情况.