【问题标题】:namespace not loaded in clojure pedestal命名空间未加载到 clojure 基座中
【发布时间】:2021-06-16 18:39:06
【问题描述】:

我在基座指南中使用初学者指南,但是在尝试使用命名空间时(需要“测试”),我收到以下错误消息: “用户/eval2012 (REPL:1) 处的执行错误 (FileNotFoundException)。 无法在类路径上找到 test__init.class、test.clj 或 test.cljc。”

尝试时会发生同样的事情(需要'hello)

我正在使用 lein repl。

我有一个名为 test 的目录,在 src 下有一个名为 test.clj 的文件

test/src/test.clj:

(ns test
(:require [io.pedestal.http :as http]
[io.pedesteal.http.route :as route]))

test/src/hello.clj:

(defn respond-hello [request]
{:status 200 :body “Herllo world”})

有什么想法吗?

测试/deps.edn:

:deps                                                
 {io.pedestal/pedestal.service {:mvn/version "0.5.7"}
  io.pedestal/pedestal.route   {:mvn/version "0.5.7"}
  io.pedestal/pedestal.jetty   {:mvn/version "0.5.7"}
  org.slf4j/slf4j-simple       {:mvn/version "1.7.28"}}
 :paths ["src"]}

【问题讨论】:

  • 当然,您必须向我们展示您的文件/目录结构和 project.clj,但乍一看:如果这些应该是测试,通常没有 src 下面test 目录 - 你好,但缺少 ns?
  • 没有project.clj,我只是跟着基座教程。文件结构如上所示,即:test/src/test.clj 和 test/src/hello.clj。名称 test 是任意的,它与任何测试无关。我在上面添加了deps.edn
  • 那你不应该使用lein repl吗?如果您为 clojure cli 工具配置内容,它将找不到文件。所以你的目录结构中的第一级test 是你的项目的名称,那么你实际上是在使用其中的所有这些工具吗?供将来参考:不要在问题中这样做 - 它会让人们(比如我)感到困惑 - 只需假设项目根目录。

标签: clojure namespaces read-eval-print-loop leiningen pedestal


【解决方案1】:

clj repl 与 lein repl 不同。要使用lein repl,您需要一个 project.clj 文件。

我使用建议的clj 成功通过Pedestal's beginner guide,但使用lein repl 时出现错误:

user=> (require 'test)
Execution error (FileNotFoundException) at user/eval2006 (REPL:1).

user=> (require 'hello)
Execution error (FileNotFoundException) at user/eval2008 (REPL:1).
Could not locate hello__init.class, hello.clj or hello.cljc on classpath.

我查看了 clj 项目和 Leiningen 项目之间的区别,这是我看到的:

  • clj 使用 deps.edn。 Leiningen 将依赖项放在 project.clj 中
  • clj 有:paths ["src"]。 Leiningen 在 project.clj 中有 :main:target-path

所以要从 clj 切换到 lein repl我添加了 project.clj 文件:

(defproject pedestal "0.1.0-SNAPSHOT"
  :dependencies [[org.clojure/clojure "1.10.1"]
                 [io.pedestal/pedestal.service "0.5.7"]
                 [io.pedestal/pedestal.route "0.5.7"]
                 [io.pedestal/pedestal.jetty "0.5.7"]]
  :main ^:skip-aot test
  :target-path "target/%s")

遵循我的目录结构...

.../pedestal/src/test.clj
.../pedestal/project.clj
.../...

当我再次启动它时,我什至不需要(require 'test) 甚至(test/start)(start) 成功了,页面会加载

Leiningen 不同于准系统 clj 工具。 与指南推荐的准系统 clj 项目相比,它指向不同的启动文件(?)并引入依赖项。

根据您的问题,我没有看到提到 project.clj,所以也许这就是您需要的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    • 2011-08-31
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多