每当我启动项目时,都会发生错误:project.clj中指定的主名称空间

发布于 2025-02-08 07:31:35 字数 456 浏览 2 评论 0 原文

无论我运行哪个github项目,我都会收到相同的错误。

现在,我已经从《勇敢和真实》的《 Clojure》一书中启动了一个简单的示例,并且出现了同一程序。

(ns clojure-noob.core
  (:gen-class))

(defn -main
  "I don't do a whole lot ... yet."
  [& args]
  (println "I'm a little teapot!"))

错误是

>   PS C:\Users\danny\New folder\cftbat-code> lein run
No :main namespace specified in project.clj.

我能做什么,我在做什么错?

Whichever github project I run I get the same error.

Now I have started a simple example from the book clojure for brave and true and the same program appears.

(ns clojure-noob.core
  (:gen-class))

(defn -main
  "I don't do a whole lot ... yet."
  [& args]
  (println "I'm a little teapot!"))

error is

>   PS C:\Users\danny\New folder\cftbat-code> lein run
No :main namespace specified in project.clj.

what can i do and what i am doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不奢求什么 2025-02-15 07:31:35

这是令人惊讶的!显然,键入 lein new myproj 在结果中不包含必要的行。特别是,您的 project.clj 文件需要这样的行:

  :main myproj.core

将其更改为如下:

(defproject myproj "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
            :url "https://www.eclipse.org/legal/epl-2.0/"}
  :dependencies [[org.clojure/clojure "1.10.3"]]
  :repl-options {:init-ns myproj.core}
  :main myproj.core
  )

以及

(ns myproj.core)

(defn -main
  "I don't do a whole lot."
  [& args]
  (println "Hello, World!" args))

结果

> lein clean; lein run abc
Hello, World! (abc)

另一种选择

,我从不使用 lein new new XXX 了。取而代之的是,我只是克隆此模板项目,然后修改它。

只需查看读书我并跟随!


额外的详细信息

您还可以指定在命令行上运行的“主函数”。由于默认“ main函数”的名称为 -main ,您可以通过键入: Lein Run Run 包含 -main 函数的命名空间:

> lein clean ; lein run :main myproj.core
Hello, World! nil

如果您的“主函数”不是 -main ,则输入类似于

> lein clean ; lein run :main myproj.core/foo

汇编失败(尤其是涉及宏)时的内容,删除所有以前的所有*。类文件通过 Lein Clean 。如果您不这样做,也许一百次,您将获得无法解释的非常奇怪的失败。

我发现定义并始终使用别名最安全:

alias lcr="lein do clean, run"
alias lct="lein do clean, test"

do 的版本比 lein Clean快1秒; Lein运行,但不要忘记逗号!


如何避免

确定,我只记得一种避免问题的方法。 JUST类型:

lein new app myapp

行。

  :main ^:skip-aot myapp.core

这将包括当您仅键入 lein新myproj (即带有 app app part)时所缺少的

This is surprising! Apparently typing lein new myproj does not include a necessary line in the result. In particular, your project.clj file needs a line like this:

  :main myproj.core

Change it to look like the following:

(defproject myproj "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
            :url "https://www.eclipse.org/legal/epl-2.0/"}
  :dependencies [[org.clojure/clojure "1.10.3"]]
  :repl-options {:init-ns myproj.core}
  :main myproj.core
  )

and

(ns myproj.core)

(defn -main
  "I don't do a whole lot."
  [& args]
  (println "Hello, World!" args))

with result

> lein clean; lein run abc
Hello, World! (abc)

Another option

Having said that, I never use lein new xxx anymore. Instead, I just clone this template project, then modify it.

Just check out the README and follow along!


Extra details

You can also specify the "main function" to run on the command line. Since the name of the default "main function" is -main, you can tell lein run the namespace containing the -main function by typing:

> lein clean ; lein run :main myproj.core
Hello, World! nil

If your "main function" is not -main, then type something like

> lein clean ; lein run :main myproj.core/foo

When you have compilation failues (especially if macros are involved), it is always safest to delete all previous *.class files via lein clean. If you don't, maybe one in a hundred times you will get very strange failures that cannot be explained.

I find it safest to define and always use an alias:

alias lcr="lein do clean, run"
alias lct="lein do clean, test"

The version with do is about 1 second faster than lein clean; lein run, but don't forget the comma!


How to avoid

OK, I just remembered a way to avoid the problem. Just type:

lein new app myapp

This will include the line

  :main ^:skip-aot myapp.core

which was missing when you just type lein new myproj (i.e. with out the app part).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文