Hi,

Your problem is probably that you only export the clj file. But the clj file
corresponds to a namespace with a gen-class. A gen-class must be compiled.
And yours is probably not.

The more I use clojure with java interop, the less I'm tempted to play with
gen-class when I'm not forced to.
Why ? Because there is this extra-step to be incorporated in your
development environment, and in your build scripts : AOT compilation.

For your particular example, where you're creating an gen-class but for
which you create a static method, I'd strongly suggest to invoke clojure
from java :

  * Either directly if it's ok
  * Either by introducing a level of indirection via a "facade" java class
which will be called by the rest of your java code.

Something like that :

Change your namespace tiny to:

(ns com.tiny)

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


Create a TinyFacade java class :

package com.tiny;

import clojure.lang.RT;
import clojure.lang.Var;

public final class TinyFacade {
    public static final Var binomialVar = RT.var("com.tiny", "binomial");
    public static double binomial(int x, int y) {
            this.binomialVar.invoke(x, y);
    }
}
Or if you don't care much about the java signature, create a utility class
for abstracting away the clojure.lang details of how to get a var:
package clojure.util;
import clojure.lang.RT;
import clojure.lang.Var;
public final class ClojureInterop {
    public static Var findVar(String ns, String name) { return RT.var(ns,
name); }
}

And in your client java class:
import clojure.util;
public class SomeClass {
  private static final Var binomial = ClojureInterop.findVar("com.tiny",
"binomial");
  public void someMethod( ... ) {
      ....
     binomial.invoke( ..., ...);
  }
}
And if you want to create a main entry point, either write it in clojure in
a namespace and use clojure script launching facility (don't know exactly
what to use), or write it in the facade, or in plain java, whatever ...

Or, use your initial code, use lein, declare the namespace to be aot, and
create the jar from lein.

HTH,

-- 
Laurent

2011/3/31 Christian <soulbea...@gmail.com>

> I've posted this question on StackOverflow:
>
> http://stackoverflow.com/questions/5452665/how-to-call-clojure-code-from-java
>
> It's still not working. I thought a crowd focused on Clojure may be
> able to help me out. Here's what I've done so far.
>
> I have used the example code from this answer:
> http://stackoverflow.com/questions/2181774/calling-clojure-from-java
>
> I created a clojure Project called tiny. I created the file 'tiny.clj'
> in the package 'com'.
>
> I have created two exports as .jar. Once, I have only exported
> 'tiny.clj' and once I exported the entire project.
>
> I create a new Java Project called Main. In the default package I
> created a file 'Main.java' that uses 'import com.tiny'.
>
> I alternate importing the two .jar files. I try importing just the
> single-.clj .jar as well as the project .jar. There is no difference.
> Eclipse reports, "Imports cannot be resolved." To import, I right-
> click on the Java Project and go to 'Build Path -> Import External
> Archives'.
>
> I also import clojure.jar and try rearranging the dependency order;
> sometimes I place the Main.java file on top, sometimes clojure.jar,
> etc. It's still not working!
>
> I followed everything to the letter seemingly. Is there anything I
> could be missing?
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to