8

I am very interested in Python for programming on the JVM, and I've worked in Java and Ruby (not JRuby) a lot, and to a certain extent in Groovy.

What are the limits to the Java-Jython and Java-JRuby integration right now? What about Groovy: are there any things that a Java class can do that a Groovy class cannot? And what about IDE integration (mostly Eclipse, but IntelliJ and Netbeans are also interesting)?

I'm not interested in whether Python, Ruby or Groovy is better/more-concise/whatever. I'm just interested in how well they are working in the JVM and tools (especially IDEs) right now. My main interest is not inside a web server, if that matters.

Simple examples of getting a HelloWorld which extends a custom Java class and implements a custom Java interface would be very useful to me.

Dan Rosenstark
  • 2,344
  • 1
  • 20
  • 20

3 Answers3

7

I have no experience of JRuby nor Groovy. But Jython:

  • Excellent integration with NetBeans. NetBeans can run Python programs with Jython almost straight out of the box, just install the "Jython Distribution" plugin and you're done. Apparently Eclipse works as well. See chapter Using Jython in an IDE in the Jython Book.

  • Java-Jython integration is excellent. Calling Java from Jython is super easy. Calling Jython from Java is not quite as straightforward (Java was not designed with Jython in mind), but still fairly easy. I've mostly had luck with creating a class in Jython (extending a Java class/interface), and then creating instances of that class in Java using an object factory. See Jython and Java Integration for how-to.

Extending a Java class in Jython works like this:

from javax.swing import JFrame, JButton

class MyFrame(JFrame):

    def __init__(self):
        self.defaultCloseOperation = JFrame.EXIT_ON_CLOSE
        self.size = (300, 300)
        self.add(JButton('Click Me!', actionPerformed=self.print_something))
        self.visible = True

    def print_something(self, event):
        print 'Clicked!'

Implementing interfaces works similarly, just import the interface, "extend" it using a class definition such as class MyClass(MyInterface) and implement what's needed.

My only criticism against Jython is that its raw performance is not very good (and that's mostly because it uses massive reflection to interact with Java). But then, raw performance is usually pretty irrelevant for a scripting language.

Joonas Pulakka
  • 23,534
  • 9
  • 64
  • 93
  • Great stuff +1. Any idea how Jython performance compares to Groovy's (also massively reflection-based) performance? – Dan Rosenstark Oct 22 '10 at 07:01
  • I haven't personally compared them, but Google seems to find lots of opinions: www.google.com/#q=jython+vs+groovy+performance – Joonas Pulakka Oct 22 '10 at 07:32
  • Google? Who's that? Looks like jython is worse than any of the other non-Java JVM langs. Interesting. – Dan Rosenstark Oct 22 '10 at 15:05
  • People use Python for full-fledged desktop apps, so performance matters somewhat. Are you sure that Jython uses the Java reflection APIs. (Are you sure it hasn't adopted the techniques that Groovy and JRuby have? See http://blog.headius.com/2008/05/power-of-jvm.html and **many** other posts on the same blog for more discussion of performance.) – Ken Bloom Dec 16 '10 at 14:32
  • @Ken Bloom: Yes, I wouldn't use Jython for full-fledged desktop apps, because it's quite slow (but I am using it embedded inside full-fledged Java apps). I'm quite sure that Jython uses mostly reflection APIs - unlike Groovy or JRuby, Jython's code base actually originates from the last millennium - although they may have made some optimizations lately. Thanks for the interesting blog link! – Joonas Pulakka Dec 16 '10 at 14:49
  • @Joonas Pulakka. http://wiki.jvmlangsummit.com/images/1/14/Jython_-_JVM_Language_Summit.pdf They really didn't follow JRuby and Groovy's techniques. Looks like they're trying to skip that generation of optimizations and target JSR292. – Ken Bloom Dec 16 '10 at 15:16
  • (6 years later!) are you sure Jython is so slow? The start-up time when you run anything is (still) a bit of a killer, but some benchmarks I looked at a few months ago seemed to say that Jython (after start-up) could in fact be very fast. I also tried to find a solution to the slow start-up issue (by keeping many jars cached in memory), but it was beyond my abilities... – mike rodent Aug 07 '16 at 11:31
4

For the last year, I've been working a lot with Groovy (and Java as well, but more Groovy).

IDE integration: OK in Netbeans. Since Groovy is dynamic, don't expect the same level of IDE support like Java (Intelisense, Refactoring etc.). Some people say that InteliJ is even better, but my coworker who checked it out didn't like it. YMMV. Last time I checked, support in Eclipse was not as good as in Netbeans.

Groovy can be very easily integrated or mixed with Java. The syntax is relatively similar, so the mental switch doesn't hurt too much.

Especialy when used with the Grails framework, Groovy is fun to work with (IMO). I also like the similarity to JavaScript; therefore a project that mixed Groovy on the backend with JavaScript on the client doesn't require understanding two completely different languages; in many cases, it feels more like two dialects of the same language.

user281377
  • 28,352
  • 5
  • 75
  • 130
2

Regarding Groovy, a Groovy class can certainly do everything that a Java class can, since valid Java is (almost) always valid Groovy.

I've found that Eclipse and NetBeans aren't quite ready for prime-time when it comes to Groovy/Grails development. But IntelliJ is really good, if you have the money.

Eric Wilson
  • 12,071
  • 9
  • 50
  • 77