At the following link Calling clojure from java , it is illustrated how to write a piece of clojure code, whose functionality we can then invoke directly in java source code. To reproduce, we have a clojure project called tiny, which within it has a tiny.clj source file. The tiny.clj file contains the following code :
(ns tiny
(:gen-class
:name com.domain.tiny
:methods [#^{:static true} [binomial [int int] double]]))
(defn binomial
"Calculate the binomial coefficient."
[n k]
(let [a (inc n)]
(loop [b 1
c 1]
(if (> b k)
c
(recur (inc b) (* (/ (- a b) b) c))))))
(defn -binomial
"A Java-callable wrapper around the 'binomial' function."
[n k]
(binomial n k))
(defn -main []
(println (str "(binomial 5 3): " (binomial 5 3)))
(println (str "(binomial 10042 111): " (binomial 10042 111)))
)
This is then exported into a ttt.jar file, which is then added to the "Referenced Libraries" of the tinyJava project (which is a java project). Inside the tinyJava project there is a Main.java file, which has the following code :
import com.domain.tiny;
public class Main {
public static void main(String[] args) {
int j;
j = (int)tiny.binomial(5, 3);
System.out.println("(binomial 5 3): " + j);
System.out.println("(binomial 10042, 111): " + tiny.binomial(10042, 111));
}
}
The output is then :
(binomial 5 3): 10
(binomial 10042, 111): 4.9068389575068143E263
My question is, at the following point in Main.java :
j = (int)tiny.binomial(5, 3);
is it possible to step into the clojure source code? I have tried and it did not work.
Thanks
[EDIT] : the way I added the jar to the Referenced Libraries was as follows :
1)Rightclick on project tinyJava & choose properties
2)Choose : Java Build Path
3)Choose : Libraries
4)Click : "Add External Jars"
5)Then go to the location of ttt.jar
[EDIT 2] : For a scala project it seems that one can achieve this in a slightly different manner (see below), whereby one links the projects rather than explicitly exporting a jar file.