On Jan 3, 8:13 pm, "Mark Volkmann" <r.mark.volkm...@gmail.com> wrote:
> I'd like to learn how to invoke Clojure code from a Java application.
> I see at least two options.
>
> 1) From a Java application, read a text file containing Clojure code
> and invoke specific functions it defines from Java code.

Here's how I do it:

import clojure.lang.RT;
import clojure.lang.Var;
...
RT.loadResourceScript("source/file/on/classpath/here.clj");
Var myfunction = RT.var("my-namespace", "my-function");
myfunction.invoke("arg 1", "arg 2", ...);


> 2) Compile Clojure code to bytecode and use it from a Java application
> just like any other Java code.

A little trickier; but easy if your Clojure code is implementing an
existing Java interface.

(ns my.cool.library
  (:gen-class :implements [SomeJavaInterface]))

(defn -functionDefinedInInterface [this arg1 arg2 ...]
  ...)

Note: defn names for functions defined in the interface begin with
"-".  Remember every method takes an extra "this" argument.

To generate the .class file, use "(compile my.cool.library)" or
clojure.lang.Compile.

-Stuart Sierra
--~--~---------~--~----~------------~-------~--~----~
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
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