【发布时间】:2015-09-19 03:46:17
【问题描述】:
假设我创建了一个新的 Leiningen 项目 (lein new app example) 并在 example/src/example/core.clj 中添加了一些使用 :gen-class 的代码:
(ns example.core
(:gen-class :extends javafx.application.Application))
(defn -start [this stage]
(.show stage))
(defn -main [& args]
(javafx.application.Application/launch example.core args))
如果我创建一个 JAR (lein uberjar) 并运行它,一切正常。但是,如果我尝试直接运行我的应用程序 (lein run),我会得到一个 ClassNotFoundException。另外,如果我打开一个 REPL (lein repl),我首先会得到和以前一样的错误,但是在运行这段代码之后:
(compile 'example.core)
lein run 中不再出现错误或lein repl。
有人可以向我解释一下这里到底发生了什么,以及如何直接运行我的应用程序而无需从 REPL 手动编译我的代码?
编辑:又胡闹了一下,发现解决这个问题的方法是添加
:aot [example.core]
到project.clj。 (感谢@Mars!)不过,我仍然很困惑,因为我之前尝试过简单地删除^:skip-aot,这(根据the docs)应该可以工作:
这将默认为 AOT 编译;要禁用此功能,请附加
^:skip-aot元数据到命名空间符号。
但事实并非如此。为什么?
另一个编辑(如果我应该将其中任何一个分成一个单独的问题,请告诉我,我会这样做):我一直在玩连字符 (lein new app my-example),并且奇怪的事情一直在发生。这不起作用:
(ns my-example.core
(:gen-class :extends javafx.application.Application))
;; ...
(defn -main [& args]
(javafx.application.Application/launch my-example.core args))
但这确实:
(ns my-example.core
(:gen-class
:name my-example.Core
:extends javafx.application.Application))
;; ...
(defn -main [& args]
(javafx.application.Application/launch my-example.Core args))
所以我的班级名称既可以以小写字母 (example.core) 开头,也可以包含连字符 (my-example.Core),但不能两者兼有? 我真的不明白。 p>
最后,lein uberjar 在最后一个示例中失败(使用显式 :name),因为
Warning: The Main-Class specified does not exist within the jar. It may not be executable as expected. A gen-class directive may be missing in the namespace which contains the main method.
据我所知,解决此问题的唯一方法是将Application 子类拆分为单独的命名空间。还有其他方法吗?
【问题讨论】:
-
我认为问题可能是您的 project.clj 中没有
:aot语句来告诉 Leiningenexample.core需要事先编译——在使用之前。如果其他班级正在使用这个班级,那就是问题所在。如果没有,那么在编译-main之前,可能该类必须存在。当您制作 uberjar 时,Leiningen 可能会以不同的顺序进行编译。我不确定,但我敢打赌是这样的。
标签: clojure leiningen gen-class