【问题标题】:Could not locate compojure in classpath无法在类路径中找到组合
【发布时间】:2011-02-23 23:07:25
【问题描述】:

我正在尝试各种入门示例,我可以得到一个基本的 hello world 示例,在路由中使用基本 HTML

(ns hello-world
  (:use compojure.core ring.adapter.jetty)
  (:require [compojure.route :as route]))

(defroutes example
  (GET "/" [] "<h1>Hello World Wide Web!</h1>"))

(run-jetty example {:port 8080})

但是当我尝试像这样使用 html 助手时

(ns hello-world
  (:use compojure ring.adapter.jetty)
  (:require [compojure.route :as route]))

(defroutes example
  (GET "/" []
    (html [:h1 "Hello World"])))

(run-jetty example {:port 8080})

然后我得到以下错误

[null] 线程“main”java.io.FileNotFoundException 中的异常:无法在类路径上找到 compojure__init.class 或 compojure.clj:(core.clj:1)

【问题讨论】:

  • 你可能会提到 (:use compojure.core ring.adapter.jetty) 而不是 (:use compojure ring.adapter.jetty)?
  • 内部网络中仍有许多示例,包括 (:use compojure)。这似乎是 Compojure 0.4 之前的示例,如下所述。上面的问题仍然代表人们仍然遵循这些例子。
  • 我认为您的第二个示例缺少“[]”。它应该是 (GET "/" [] ...

标签: clojure installation compojure


【解决方案1】:

我玩过 Compojure “Hello World” 并且遇到了这个问题(以及许多其他在我的大脑中变得混乱的问题)。另一个问题是网络上的很多 Compojure 文档已经过时了。最重要的是,这些是您要遵循的步骤:

  1. 拥有最新版本的Leiningen。确保遵循 github 站点上的安装说明。 (不要通过 macports;他们的 Leiningen 已经过时了。)

  2. 遵循 Compojure 指令here

注意文件名不正确。它应该是 src/hello_www/core.clj 而不是 src/hello-www/core.clj。

【讨论】:

  • 感谢 Julian,这是我可以开始工作的示例。然而,我不能包含 (html [:h1 "foo"]) 的示例。从 Compojure 0.4.0 版开始,现在似乎需要 hiccup.core。
【解决方案2】:

正如 W55tKQbuRu28Q4xv 在评论中提到的那样,您在第二个示例中使用 (:use compojure ...)。您应该切换到(:use compojure.core ...),然后可能为您使用的其他功能引入一些额外的依赖项(例如hiccup

我的猜测是您在使用 Compojure 0.4 时尝试遵循一些为 Compojure 0.3 编写的教程。后者根本不包括compojure 命名空间,并且已经精简了很多,基本的HTTP 处理委托给ring 和其他各种功能分拆到单独的项目(如前面提到的hiccup)。

幸运的是,在 0.3 -> 0.4 过渡上有很好的资源,例如this blog entry 布伦顿阿什沃思。如果您无法找到已从 Compojure 中删除的内容,那么您很有可能现在可以从中了解在哪里可以找到它。另请参阅 Compojure 的 Google 小组上的 this follow-up discussion,了解勘误表和其他详细信息。

【讨论】:

  • 谢谢迈克尔,就是这样。我需要包含 hiccup.core 才能使 (html [:h1 "foo"]) 工作正常。
猜你喜欢
  • 2019-05-03
  • 2012-09-25
  • 2015-01-08
  • 1970-01-01
  • 2015-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-12
相关资源
最近更新 更多