从Java调用Clojure 1.3

3

我希望能够从Java代码中调用Clojure函数。虽然这个问题之前已经被问过,但是现有的答案对我来说并不起作用。(使用Clojure 1.3和leiningen 1.7.0)。我有以下最小程序:

(ns thing.main
  (:gen-class
    :methods [#^{:static true} [foo [int] void]]))

(defn -foo [i] (println "hello world, input is " i))

这个项目的.clj文件看起来像这样:

(defproject thing "1.0.0-SNAPSHOT"
  :dependencies [[org.clojure/clojure "1.3.0"]]
  :aot [thing.main]
  :omit-source true)

我生成了一个Uberjar并将其复制到与此小型Java程序相同的目录中。

import thing.*;
class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!");
        main.foo(5);  // or, alternately, foo(5);
    }
}

我使用以下命令进行编译:

javac -cp '.:thing-1.0.0-SNAPSHOT-standalone.jar' HelloWorldApp.java

编译成功,但运行程序失败(ClassNotFoundException)。直接调用foo的第二种形式,如foo(5),甚至无法编译通过。我还尝试了在:gen-class中使用和不使用"static"声明。
1个回答

1

当我使用指定类路径运行程序时,似乎对我有效。尝试像这样:

java -cp '.:thing-1.0.0-SNAPSHOT-standalone.jar' HelloWorldApp

你说得对--我没有使用完整的-cp选项来调用Java。 - sandover

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接