代码之家  ›  专栏  ›  技术社区  ›  Alex

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

  •  0
  • Alex  · 技术社区  · 6 年前

    我正在学习Clojure和《Clojure为勇敢和真实》一书,并使用emacs、cider和leiningen我创建了一个项目。

    lein new app the-divine-cheese-code
    

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

    神圣奶酪代码 神圣奶酪代码

    在“core.clj”中,我指的是“svg.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)))
    

    神圣奶酪代码

    (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             
    

    神圣奶酪代码\目标\默认\过时
    神圣奶酪代码\target\default\stale\leiningen.core.classpath.extract-native-dependencies
    神圣奶酪代码/测试
    神圣奶酪代码\测试\神圣奶酪代码
    神圣奶酪代码\测试\神圣奶酪代码\核心测试.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的主要操作系统。

    经过一番试验,我发现苹果酒在没有

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

    这看起来像苹果酒的臭虫,在这种情况下,“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
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Aleph Aleph    6 年前

    建议使用 ns 宏而不是裸 require refer 语句-这是在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 ...)
    
        2
  •  0
  •   Herman Nurlygayanov    5 年前

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

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

    推荐文章