首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在emacs/cider中打开leiningen项目会引发类路径错误

在emacs/cider中打开leiningen项目会引发类路径错误
EN

Stack Overflow用户
提问于 2018-11-13 19:36:06
回答 2查看 110关注 0票数 2

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

代码语言:javascript
复制
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

代码语言:javascript
复制
(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

代码语言:javascript
复制
(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)))

下面是项目的整个dir结构。

代码语言:javascript
复制
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             

divine stale code\target\default\stale

代码语言:javascript
复制
 the-divine-cheese-code\target\default\stale\leiningen.core.classpath.extract-native-dependencies
代码语言:javascript
复制
 the-divine-cheese-code\test
代码语言:javascript
复制
 the-divine-cheese-code\test\the\_divine\_cheese\_code
代码语言:javascript
复制
 the-divine-cheese-code\test\the\_divine\_cheese\_code\core\_test.clj

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

代码语言:javascript
复制
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就能成功地编译源代码。

代码语言:javascript
复制
(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的主要操作系统。

经过一些实验后,我发现苹果酒工作没有:

代码语言:javascript
复制
(ns the-divine-cheese-code.core
  (:require [the-divine-cheese-code.visualization.svg]))

这看起来像是苹果酒的bug,在这种情况下'lein run‘可以预见会产生错误。如果我用正确的方式

代码语言:javascript
复制
(ns the-divine-cheese-code.core
  (:require [the-divine-cheese-code.visualization.svg :refer :all]))

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

代码语言:javascript
复制
Caused by java.lang.IllegalStateException
latlng->point already refers to:
#'the-divine-cheese-code.svg/latlng->point in namespace:
the-divine-cheese-code.core
EN

回答 2

Stack Overflow用户

发布于 2018-11-14 19:06:06

我建议使用Clojure宏提供的选项,而不是裸露的requirerefer语句-这是在ns中执行导入/请求的推荐方式,大多数工具都是按照这种管理名称空间的方式编写的。即使下面的代码在苹果酒中仍然不起作用,它也会更容易诊断:

代码语言:javascript
复制
;; 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 ...)
票数 0
EN

Stack Overflow用户

发布于 2019-06-18 22:03:04

在我的例子中,当我递归地重命名src/the_divine_cheese_code/文件夹及其内容后,错误就消失了。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53280177

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档