使用外部依赖项加载my-project.core时出现FileNotFoundException

时间:2012-03-28 17:16:25

标签: clojure read-eval-print-loop leiningen

在尝试使用外部库时,我在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

2 个答案:

答案 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))