2012-03-23 46 views
3

我想從Java代碼調用Clojure函數。這個問題最近沒有被問到,現有的答案對我來說並不合適。 (Clojure 1.3,Leiningen 1.7.0)。我有以下幾個最基本的程序:從Java調用Clojure 1.3

(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。

回答

1

當我使用指定的類路徑運行程序時,似乎適用於我。嘗試是這樣的:

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

你說得對 - 我沒有使用完整的-cp選項調用java。 – sandover 2012-03-23 23:02:09