3

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.

1 Answer 1

1

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 
Sign up to request clarification or add additional context in comments.

1 Comment

You're right -- I was not invoking java with the full -cp option.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.