[英]gen-class not generating a class
我在引用通过:gen-class
生成的类时遇到困难。
我可以显示的最小的示例说明了这个问题:
(defproject test-proj
:dependencies [[org.clojure/clojure "1.8.0"]]
:aot [test-proj.test])
(ns test-proj.test
(:gen-class))
(defn -main []
(println test_proj.test)) ; Error here
问题是,这会在标记的行上产生ClassNotFoundException
。
(我在上面的文件和project.clj
尝试了-
和_
所有不同组合。我仍然不完全了解需要使用下划线和容忍破折号的功能。有些东西似乎会随破折号一起滚动,并根据需要进行转换,而我知道,从插科打诨,在-main
,我需要下划线参考test_proj.test
。)
如果我进入项目的根文件,则没有target
文件夹,因此不会生成类。 如果我进入终端并运行lein compile
,它会在target
下生成所需的类,并且上面的代码运行没有错误。 但是,这是一个较差的解决方法。 如果我修改了文件却忘记了手动重新编译怎么办? 我每次clean
后都必须手动编译它也是一种痛苦。
在黑暗中拍摄时,我尝试在ns
宏下面使用compile
:
(compile 'test-proj.test)
如果我使用破折号,那么compile
似乎什么也没做。 我可能会误解其用法,但它不会在target
之下生成类文件。 如果使用下划线,则会出现异常,表明找不到名称空间。
有没有一种方法可以自动生成类,所以我不需要每次都运行lein compile
? 我认为这就是project.clj
的:aot
所做的。
使用Leiningen,指定:aot设置。 :all是最简单的。
project.clj
(defproject test-proj "0.1.0-SNAPSHOT"
:main test-proj.core
:aot :all
:dependencies [[org.clojure/clojure "1.8.0"]])
如果需要,可以在数组中指定确切的名称空间,如下所示:
project.clj
(defproject test-proj "0.1.0-SNAPSHOT"
:main test-proj.core
:aot [test-proj.core]
:dependencies [[org.clojure/clojure "1.8.0"]])
然后执行以下lein命令:
lein compile
将生成上述:aot设置中指定的字节码和.class文件。
core.clj
(ns test-proj.core
(:gen-class))
(defn -main[]
(println test_proj.core)
(println "Hello, World!"))
您想看到类似下面的内容:
NikoMacBook% lein compile
Compiling test-proj.core
完成此操作后,检查目标文件夹,其中包含正确的类文件,此处为test_proj / core.class。
NikoMacBook% tree target
target
├── classes
│ ├── META-INF
│ │ └── maven
│ │ └── test-proj
│ │ └── test-proj
│ │ └── pom.properties
│ └── test_proj
│ ├── core$_main.class
│ ├── core$fn__38.class
│ ├── core$loading__5569__auto____36.class
│ ├── core.class
│ └── core__init.class
└── stale
└── leiningen.core.classpath.extract-native-dependencies
7 directories, 7 files
以下将运行:main命名空间,因此运行test-proj.core。
lein run
将输出
NikoMacBook% lein run
Compiling test-proj.core
Compiling test-proj.core
test_proj.core
Hello, World!
注意,该类正在调用自身。 还请注意,如果您不预先运行lein compile,它将自行运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.