【问题标题】:Calling Clojure 1.3 from Java从 Java 调用 Clojure 1.3
【发布时间】:2012-03-23 21:20:17
【问题描述】:

我想从 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 中使用和不使用“静态”声明。

【问题讨论】:

    标签: clojure clojure-java-interop


    【解决方案1】:

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

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

    【讨论】:

    • 你说得对——我没有使用完整的 -cp 选项调用 java。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 1970-01-01
    • 2014-01-13
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多