在我的 Leiningen 项目中:
(defproject com.stackoverflow.clojure/tests "0.1.0-SNAPSHOT"
:description "Tests of Clojure test-framework."
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.6.0"]
[instaparse "1.3.4"]]
:aot [com.stackoverflow.clojure.testGenClass]
:source-paths ["src/main/clojure"]
:java-source-paths ["src/main/java"]
:test-paths ["src/test/clojure"]
:java-test-paths ["src/test/java"]
)
我正在使用 gen-class 生成 Java 类:
(ns com.stackoverflow.clojure.testGenClass
(:gen-class
:name com.stackoverflow.clojure.TestGenClass
:implements [com.stackoverflow.clojure.TestGenClassInterface]
:prefix "java-"))
(def ^:private pre "START: ")
(defn java-addToString [this text post]
(str pre text post))
我想在Java中使用:
package com.stackoverflow.clojure;
public class TestGenClassTest {
private TestGenClassTest() {
}
public static void main(String[] args) {
TestGenClassInterface gc = new TestGenClass();
System.out.println(gc.addToString("Called from Java!", " :END"));
}
}
启动lein compile
会引发以下错误:
Compiling 4 source files to /home/eddy/workspace/TestSkripts/target/classes
/home/eddy/workspace/TestSkripts/src/main/java/com/stackoverflow/clojure/TestGenClassTest.java:9: error: cannot find symbol
TestGenClassInterface gc = new TestGenClass();
^
symbol: class TestGenClass
location: class TestGenClassTest
1 error
在我看来,在编译 Java 代码(此处:)期间,TestGenClassTest
该类不可用。我通常做的是
- 注释掉那些使用 gen-class 生成类的部分(这里
TestGenClass
) - 运行
lein compile
(生成类文件) - 然后再次输入注释掉的代码
- 并
lein compile
再次运行。
我肯定有更好的方法,可以让所有手动步骤变得多余。