【问题标题】:Opening leiningen project in emacs/cider raises classpath error在 emacs/cider 中打开 leiningen 项目会引发类路径错误
【发布时间】:2018-11-13 11:36:05
【问题描述】:

我正在通过“Clojure for the Brave and True”一书学习 Clojure,并使用 emacs、cider 和 leiningen。我已经创建了一个项目。

lein new app the-divine-cheese-code

然后我已将源文件添加到项目中。

the-divine-cheese-code\src\the_divine_cheese_code\core.clj the-divine-cheese-code\src\the_divine_cheese_code\visualization\svg.clj

在“core.clj”中,我指的是“svg.clj”命名空间。

the-divine-cheese-code\src\the_divine_cheese_code\core.clj

(ns the-divine-cheese-code.core)
;; Ensure that the SVG code is evaluated
(require 'the-divine-cheese-code.visualization.svg)
;; Refer the namespace so that you don't have to use the 
;; fully qualified name to reference svg functions
(refer 'the-divine-cheese-code.visualization.svg)

(def heists [{:location "Cologne, Germany"
              :cheese-name "Archbishop Hildebold's Cheese Pretzel"
              :lat 50.95
              :lng 6.97}
             {:location "Zurich, Switzerland"
              :cheese-name "The Standard Emmental"
              :lat 47.37
              :lng 8.55}
             {:location "Marseille, France"
              :cheese-name "Le Fromage de Cosquer"
              :lat 43.30
              :lng 5.37}
             {:location "Zurich, Switzerland"
              :cheese-name "The Lesser Emmental"
              :lat 47.37
              :lng 8.55}
             {:location "Vatican City"
              :cheese-name "The Cheese of Turin"
              :lat 41.90
              :lng 12.45}])

(defn -main
  [& args]
  (println (points heists)))

the-divine-cheese-code\src\the_divine_cheese_code\visualization\svg.clj

(ns the-divine-cheese-code.visualization.svg)

(defn latlng->point
  "Convert lat/lng map to comma-separated string" 
  [latlng]
  (str (:lat latlng) "," (:lng latlng)))

(defn points
  [locations]
  (clojure.string/join " " (map latlng->point locations)))

这是项目的整个目录结构。

the-divine-cheese-code              
the-divine-cheese-code\.gitignore               
the-divine-cheese-code\.hgignore                
the-divine-cheese-code\.nrepl-port              
the-divine-cheese-code\CHANGELOG.md             
the-divine-cheese-code\doc              
the-divine-cheese-code\doc\intro.md             
the-divine-cheese-code\LICENSE              
the-divine-cheese-code\project.clj              
the-divine-cheese-code\README.md                
the-divine-cheese-code\resources                
the-divine-cheese-code\src              
the-divine-cheese-code\src\the_divine_cheese_code               
the-divine-cheese-code\src\the_divine_cheese_code\core.clj              
the-divine-cheese-code\src\the_divine_cheese_code\visualization             
the-divine-cheese-code\src\the_divine_cheese_code\visualization\svg.clj             
the-divine-cheese-code\target               
the-divine-cheese-code\target\default               
the-divine-cheese-code\target\default\classes               
the-divine-cheese-code\target\default\classes\META-INF              
the-divine-cheese-code\target\default\classes\META-INF\maven                
the-divine-cheese-code\target\default\classes\META-INF\maven\the-divine-cheese-code             
the-divine-cheese-code\target\default\classes\META-INF\maven\the-divine-cheese-code\the-divine-cheese-code              
the-divine-cheese-code\target\default\classes\META-INF\maven\the-divine-cheese-code\the-divine-cheese-code\pom.properties
the-divine-cheese-code\target\default\repl-port             

the-divine-cheese-code\target\default\stale
the-divine-cheese-code\target\default\stale\leiningen.core.classpath.extract-native-dependencies
神圣奶酪代码\测试
the-divine-cheese-code\test\the_divine_cheese_code
the-divine-cheese-code\test\the_divine_cheese_code\core_test.clj

当我使用“lein run”运行项目时,它会成功执行。但是,当我用 emacs/cider 打开 core.clj 文件并尝试编译它时,我得到了类路径错误。

CompilerException java.io.FileNotFoundException: Could not locate 
the_divine_cheese_code/visualization/svg__init.class or 
the_divine_cheese_code/visualization/svg.clj on classpath. Please check 
that namespaces with dashes use underscores in the Clojure file name., 
compiling:(c:/temp/the-divine-cheese-code/src/the_divine_cheese_code/core.clj:2:1)

如果我将它们放在同一个目录中(相应地更改命名空间),CIDER 会成功编译源代码。

(ns the-divine-cheese-code.core)
(require 'the-divine-cheese-code.svg)
(refer 'the-divine-cheese-code.svg)

(def heists [{:location "Cologne, Germany"
    ...

因此,问题可能与操作系统有关。我使用 Windows 7,它不是 Emacs/CIDER 的主要操作系统。

经过一些实验,我发现 CIDER 可以在没有 :refer 的情况下工作

(ns the-divine-cheese-code.core
  (:require [the-divine-cheese-code.visualization.svg]))

这看起来像一个 CIDER 错误,并且在这种情况下,“lein run”可以预见地给出错误。 如果我做对了

(ns the-divine-cheese-code.core
  (:require [the-divine-cheese-code.visualization.svg :refer :all]))

苹果酒现在给出另一个错误:

Caused by java.lang.IllegalStateException
latlng->point already refers to:
#'the-divine-cheese-code.svg/latlng->point in namespace:
the-divine-cheese-code.core

【问题讨论】:

  • 你试过改用ns macro吗?
  • @AlephAleph 我不太明白这个评论。我已经在两个源文件中使用了 ns 宏。此示例应用程序的关键点是从另一个名称空间引用一个名称空间。另外,让我再次注意,该项目使用“lein run”成功执行。因此,在这种情况下,命名空间已成功解析。我已经编辑了问题并添加了完整的来源。

标签: cider


【解决方案1】:

建议使用ns 宏提供的选项,而不是裸requirerefer 语句 - 这是在 Clojure 中执行导入/要求的推荐方式,并且大多数工具都是用这种方式编写的记住管理命名空间。即使下面的代码在 CIDER 中仍然不起作用,也更容易诊断:

;; the-divine-cheese-code\src\the_divine_cheese_code\core.clj

(ns the-divine-cheese-code.core
  (:require [the-divine-cheese-code.visualization.svg :refer :all]))

(def heists ...)

(defn- main ...)

【讨论】:

  • 如您所料,这在 CIDER 中仍然不起作用。错误是一样的。
  • 您是否使用cider-jack-in 连接到 CIDER 中的 REPL?
  • 是的,我使用 cider-jack-in。我还在问题中添加了更多信息。
  • 这条消息latlng->point already refers to: #'the-divine-cheese-code.svg/latlng->point 可能意味着您有一个文件the_divine_cheese_code/svg.clj,然后将其内容移动到另一个文件。您是否尝试过重新启动 CIDER repl?
  • 我尝试了 'cider-quit'、'cider-jack-in',然后我得到了原始类路径错误。
【解决方案2】:

在我的例子中,在我重命名(同名)src/the_divine_cheese_code/ 文件夹并且它的内容递归之后,错误就消失了。

不确定是什么导致了问题。我的猜测是字符编码被搞砸了。我在 Emacs 中创建了文件和文件夹,并在 Double Commander 中完成了重命名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-10
    • 2015-01-18
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多