首页 > 解决方案 > 在 emacs/cider 中打开 leiningen 项目会引发类路径错误

问题描述

我正在通过“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\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 的主要操作系统。

经过一些实验,我发现苹果酒在没有 :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

标签: cider

解决方案


建议使用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 ...)

推荐阅读