문제

I'd like to call a Clojure function from Java code. This question hasn't been asked recently, and the existing answers aren't working for me. (Clojure 1.3, leiningen 1.7.0). I have the following minimal program:

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

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

The project .clj looks like this:

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

I generate an uberjar, and I copy it into the same directory with this little Java program.

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

I compile using this command:

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

Compilation succeeds, but the program fails when it's run (ClassNotFoundException). The second form of invoking foo directly, as foo(5), doesn't even compile. I've also tried it with and without the "static" declaration in :gen-class.

도움이 되었습니까?

해결책

Seems to work for me when I am running the program with classpath specified. Try like this:

java -cp '.:thing-1.0.0-SNAPSHOT-standalone.jar' HelloWorldApp
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top