Can you mix the JVM languages? ie: Groovy & Clojure
Asked Answered
P

6

12

I understand that you can easily mix groovy&java, clojure&java, whateverJvmLang&java.

Does this also mean I can have clojure and groovy code interact as well? If I use Grails or jRoR, can I also make use of clojure in that environment?

Perichondrium answered 26/2, 2009 at 17:3 Comment(0)
M
15

As long as the languages in question actually produce Java classes (that is: classes that are known to the Java platform, not necessarily written in the Java language), then yes: they can easily interact with each other.

At least for Groovy I know that it's entirely possible to write classes that can be used from "normal" Java code.

Mcgannon answered 26/2, 2009 at 17:16 Comment(1)
It's also possible to use Clojure generated classes in Java code and vice versa: so the whole Clojure <-> Java <-> Groovy path should workCima
F
14

Clojure can be compiled to .class files via gen-class and used from Java like any other code; see here.

Fasten answered 26/2, 2009 at 18:29 Comment(0)
O
4

There is also an example on how you can call Clojure code from JRuby.

Octopod answered 27/2, 2009 at 4:38 Comment(0)
I
3

As has been mentioned, Clojure can interact easily with Java. Although many of the examples you see show how to do it from the REPL, once you compile Clojure, or any other JVM language, to a class or jar file, it can be called from Java just like any other class.

In my case, the difficulty (not much of one) was figuring out how to use gen-class correctly. There is a small set of statistical functions written in Clojure at http://kenai.com/projects/binomialstats that illustrates how to do it. That Clojure library is used by a Java project -- signtest. (Sorry, since I'm new, StackOverflow won't let me post multiple links.)

What would really be nice now is if some of the IDEs could deal with different languages in the same project. The two projects mentioned above are maintained as two individual NetBeans projects for example. Assuring that the two parts are always synchronized can be a bit of extra work.

Inspiratory answered 6/10, 2009 at 14:52 Comment(0)
A
2

As already mentioned, you can access Clojure classes from Groovy via pre-compilation.

You can access most popular scripting languages via Java 6's scripting API as well, though Scala and Clojure don't seem to be officially supported. Here are examples from Groovy:

http://groovy.codehaus.org/JSR-223+access+to+other+JVM+languages

You can also get access to Clojure's classes from Groovy, e.g. (for Groovy 1.7 snapshot):

@Grab(group='org.clojure', module='clojure', version='1.0.0')
import clojure.lang.*

def ss = StringSeq.create('The quick brown fox')
def done = false
while (!done) {
  println ss.first()
  ss = ss.next()
  done = !ss
}

Or interact via creating a new Process (again for Groovy 1.7 snapshot):

@Grab(group='org.clojure', module='clojure', version='1.0.0')
import clojure.lang.Script

def src = new File('temp.clj')
src.text = '''
(defn factorial [n]
   (if (< n 2)
       1
       (* n (factorial (- n 1)))))
(println (factorial 4))
'''
def path = System.getProperty('user.home') + '/.groovy/grapes/org.clojure/clojure/jars/clojure-1.0.0.jar'
new AntBuilder().with {
    java(classname:Script.name, classpath:path) {
        arg(value:src.path)
    }
}

There is also a Clojure plugin for Grails which provides easy access to execute clojure code from any Grails artifact (controllers, taglibs, services etc...):

http : / / grails.org/plugin/clojure

Adlar answered 28/7, 2009 at 0:47 Comment(1)
just to comment on the grails plugin part and say that I have used the plugin in production without a glitch, and great joy.Fideicommissary
M
1

Another environment which facilitates mixing languages on the JVM is WebSphere sMash (aka Project Zero). It includes:

Moleskin answered 18/10, 2009 at 18:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.