JRuby can access classes implemented in pure Java code. Clojure can generate a class file. Therefore it's straightforward for JRuby to access classes implemented in Clojure.
First I define a class with a method in plain Java.
MrJava.java:
and compile it.
$ javac MrJava.java
Now you have MrJava.class.
Next I defina class with a method in Clojure.
MsClojure.clj:
This is almost equivalent to the previous code. Try compiling it.
$ java -cp ~/git/clojure/clojure.jar:. clojure.main -e "(compile 'MsClojure)"
Note that you specified your own clojure.jar. Nou you have
classes/MsClojure.class
... not ./MsClojure.class
.
Try combining them with JRuby!
a.rb:
and run by:
$ jruby -J-cp ~/git/clojure/clojure.jar:classes:.
then you will get the following message.
Good afternoon Ujihisa, how are you?
Hey Ujihisa!
- It doesn't change
$: << '~/git/clojure/clojure.jar'
in JRuby file. You have to give classpath by command line option.
It gets better; you can use Clojure's persistent data structures from JRuby as well: http://github.com/technomancy/clojure-gem
ReplyDeleteUsing the STM is still not seamless since JRuby's Exception proxies get in the way of transaction retries, but with a little more work it could be done.
Thanks Phil for letting me know it!
ReplyDelete