简体   繁体   English

gen-class不生成类

[英]gen-class not generating a class

I'm having difficulty referencing classes generated via :gen-class . 我在引用通过:gen-class生成的类时遇到困难。

The smallest example I can show that demonstrates the problem is: 我可以显示的最小的示例说明了这个问题:

(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

The problem is, this yields a ClassNotFoundException on the marked line. 问题是,这会在标记的行上产生ClassNotFoundException

(I tried all different combinations of - and _ in the above file and the project.clj . I still don't fully understand what requires an underscore and what tolerates a dash. Some things seem to roll with dashes and convert them as needed, whereas I know from messing around that in the -main , I need underscores to reference test_proj.test .) (我在上面的文件和project.clj尝试了-_所有不同组合。我仍然不完全了解需要使用下划线和容忍破折号的功能。有些东西似乎会随破折号一起滚动,并根据需要进行转换,而我知道,从插科打诨,在-main ,我需要下划线参考test_proj.test 。)

If I go into the project root file, there's no target folder, so it's not generating the class. 如果我进入项目的根文件,则没有target文件夹,因此不会生成类。 If I go into the terminal and run lein compile , it generates the needed classes under target , and the above code runs without error. 如果我进入终端并运行lein compile ,它会在target下生成所需的类,并且上面的代码运行没有错误。 This is a poor workaround though. 但是,这是一个较差的解决方法。 What if I modify the file and forget to manually recompile it? 如果我修改了文件却忘记了手动重新编译怎么办? It's also a pain to have to manually compile it after any time I do a clean . 我每次clean后都必须手动编译它也是一种痛苦。

As a shot in the dark, I tried using compile right underneath the ns macro: 在黑暗中拍摄时,我尝试在ns宏下面使用compile

(compile 'test-proj.test)

If I uses dashes, compile seems to do literally nothing. 如果我使用破折号,那么compile似乎什么也没做。 I may be misinterpreting its use, but it doesn't generate class files under target . 我可能会误解其用法,但它不会在target之下生成类文件。 If I use underscores, it gives an exception saying that the namespace isn't found. 如果使用下划线,则会出现异常,表明找不到名称空间。

Is there a way to have the classes generated automatically so I don't need to run lein compile everytime? 有没有一种方法可以自动生成类,所以我不需要每次都运行lein compile I thought that that's what the :aot in the project.clj did. 我认为这就是project.clj:aot所做的。

With Leiningen, specify :aot settings. 使用Leiningen,指定:aot设置。 :all is the easiest. :all是最简单的。

project.clj project.clj

(defproject test-proj "0.1.0-SNAPSHOT"
  :main test-proj.core
  :aot :all
  :dependencies [[org.clojure/clojure "1.8.0"]])

If you want, you can specify the exact namespaces in an array as seen below: 如果需要,可以在数组中指定确切的名称空间,如下所示:

project.clj project.clj

(defproject test-proj "0.1.0-SNAPSHOT"
  :main test-proj.core
  :aot [test-proj.core]
  :dependencies [[org.clojure/clojure "1.8.0"]])

Then the following lein command: 然后执行以下lein命令:

lein compile

Will generate the byte code and .class files as specified in the :aot settings above. 将生成上述:aot设置中指定的字节码和.class文件。

core.clj core.clj

(ns test-proj.core
    (:gen-class))

(defn -main[]
  (println test_proj.core)
  (println "Hello, World!"))

You want to see something like the below: 您想看到类似下面的内容:

NikoMacBook% lein compile 
Compiling test-proj.core

Once this is done, check the target folder, contains the proper class file, here test_proj/core.class. 完成此操作后,检查目标文件夹,其中包含正确的类文件,此处为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

The following will run the :main namespace, so test-proj.core. 以下将运行:main命名空间,因此运行test-proj.core。

lein run 

Will output 将输出

NikoMacBook% lein run 
Compiling test-proj.core
Compiling test-proj.core
test_proj.core
Hello, World!

Note, that the class is calling itself. 注意,该类正在调用自身。 Note also that if you do not run lein compile beforehand , it will run by itself. 还请注意,如果您不预先运行lein compile,它将自行运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM