我运行了 lein new app hm
,然后在 hm/src/hm
中将 core.clj
编辑为:
(ns hm.core
(:gen-class)
(:use [hm.hashmap]))
(defn -main []
(def j (new hm.hashmap))
(-add j "foo" "bar")
(println j))
和hashmap.clj
为:
(ns hm.hashmap
(:gen-class
:methods [[hashmap [] java.util.HashMap]
[add [String String]]]))
(defn -hashmap []
(def h (new java.util.HashMap))
h)
(defn -add [this key value]
(. this put key value)
this)
目标是围绕 HashMap 进行包装,以便我可以理解 Clojure 以及它与 Java 的关系。我是 Clojure 的新手。但是,当我编译它时,我在 hashmap.clj
中得到了很多 ClassNotFoundException。我怎样才能使它工作?
最佳答案
注意:这是对您问题的直接回答。我不建议您以这种方式学习 Clojure。
您需要先编译您的类,然后才能运行它们。在您的 project.clj 中将其添加到 map 中:
:aot [hm.hashmap]
然后您需要运行lein compile
来编译这些类。您应该看到输出表明 hm.hashmap
类已编译。之后运行 lein run
以调用 hm.core
中的“main”函数。
我删除了你的 gen-class 的 :methods
部分,因为你已经在下面定义了它们,这导致了奇怪的 java.lang.,
错误。您将遇到其他错误,但这应该足以让您通过此问题。
关于java - 如何在 Lein 项目中设置类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17975764/