在使用Noir编写Clojure的Web应用程序时出现主命名空间错误

11

我正在使用Noir。

这是我的project.clj文件。

(defproject noir "1.0.0-SNAPSHOT"
  :description "FIXME: write description"
  :dependencies [[org.clojure/clojure "1.3.0"]])

lein run 给我返回了这个错误:

No :main namespace specified in project.clj.

我哪里出错了?

现在,如果我在project.clj中加入:main my-website.server,我会得到以下错误:

Exception in thread "main" java.lang.RuntimeException: java.lang.ClassNotFoundException: my-website.server

at clojure.lang.Util.runtimeException(Util.java:165)
    at clojure.lang.RT.classForName(RT.java:2017)
    at clojure.lang.Reflector.invokeStaticMethod(Reflector.java:206)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at clojure.lang.Reflector.invokeMatchingMethod(Reflector.java:92)
    at clojure.lang.Reflector.invokeStaticMethod(Reflector.java:225)
    at user$eval29.invoke(NO_SOURCE_FILE:1)
    at clojure.lang.Compiler.eval(Compiler.java:6465)
    at clojure.lang.Compiler.eval(Compiler.java:6455)
    at clojure.lang.Compiler.eval(Compiler.java:6431)
    at clojure.core$eval.invoke(core.clj:2795)
    at clojure.main$eval_opt.invoke(main.clj:296)
    at clojure.main$initialize.invoke(main.clj:315)
    at clojure.main$null_opt.invoke(main.clj:348)
    at clojure.main$main.doInvoke(main.clj:426)
    at clojure.lang.RestFn.invoke(RestFn.java:421)
    at clojure.lang.Var.invoke(Var.java:405)
    at clojure.lang.AFn.applyToHelper(AFn.java:163)
    at clojure.lang.Var.applyTo(Var.java:518)
    at clojure.main.main(main.java:37)
Caused by: java.lang.ClassNotFoundException: my-website.server
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at clojure.lang.DynamicClassLoader.findClass(DynamicClassLoader.java:61)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at clojure.lang.RT.classForName(RT.java:2013)
    ... 21 more

看起来你的 my-website.server 中没有 -main 函数。你是怎么创建这个项目的?如果你使用 lein noir new my-website 命令,它会设置 project.clj 并在 server.clj 中创建一个默认的 -main 函数。 - Diego Basch
1
这就是我所做的。lein noir new appname - Hick
1个回答

9
问题在于lein不知道如何定位您的-main函数所在位置:
首先,您应该创建一个.clj文件来运行。您可以使用ns宏指定其命名空间。然后,在此命名空间中定义-main函数。
(ns my-website.server
  (:require [noir.server :as server]
            [noir.core :refer [defpage]]))

(defpage "/welcome" []
  "Welcome to Noir!")

(defn -main
  [& args]
  (server/start 4000))

然后你需要配置你的project.clj:
(defproject my-website "0.1.0-SNAPSHOT"
            :description "..."
            :dependencies [[org.clojure/clojure "1.4.0"]
                           [noir "1.2.2"]]
            :main my-website.server)

[noir "1.2.2"] 是 noire 的最新稳定版本。最好使用这个版本。

别忘了将这个文件放入源目录中。默认情况下,它位于项目根目录下的./src目录中。所以,如果你的命名空间叫做my-website.server,那么lein会在./src/my-website/server.clj文件中寻找它(或者在./src/my_website/server.clj中,我不确定)。

现在,运行lein run将使lein进入命名空间my-website.server,然后运行(-main)函数。

有关更多信息,请参见 示例lein项目

您还可以使用 lein noir模板 为您的noir项目生成project.clj


1
这个.clj文件应该叫什么?(抱歉,刚开始还有一些文档需要平滑处理。) - Hick
已经完成。当前错误: 异常线程"main" java.lang.RuntimeException: 无法解析符号:defpartial在此上下文中,编译:(noir/content/defaults.clj:8) - Hick
server.clj 中出现了一些错误。Lein 找到了它,但是编译失败了。 - Leonid Beschastny
1
如果没有帮助,请尝试将noir版本更改为[noir"1.2.2"]。 1.2.2是稳定版。 - Leonid Beschastny
我认为你应该将它放在单独的回答中,我会接受它作为答案以帮助其他人。 - Hick
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接