Groovy: Java Scripting

See below for an explanation why this is written in English

Groovy logo

A few days ago I stumbled across an article at onjava.com introducing Groovy. Groovy is a scripting language for Java, meaning that Groovy-scripts can be executed without the need of first being compiled. This makes it very easy to write little scripts which can be executed from the command-line or from inside a java application.

Groovy is not the only scripting language for Java. Others include Beanshell, Jython, JRuby and some more. A nice overview can be found here. People that use jEdit will certainly have heard of Beanshell since it is the language used by jEdit to write Macro's.

Both Groovy and Beanshell use a syntax based on Java, but their goals are a bit different. Beanshell tries to interprete normal java files and seems to do this very well. So by using Beanshell it should be possible to run a real Java application by feeding the source files to Beanshell, instead of compiling them into .class-files. The goal of Groovy is to provide an easy to use scripting language, by adding some "sugar code", to make it easier to develop with. An example: System.out.println("this is a foobar!");
println "this is another foobar!"
Both lines are valid Groovy-syntax and do exactly the same: print a line to the screen. However the second line, in "Groovy-style" is much easier then the first line, which uses regular Java syntax.

Another example of sugar code is some kind of 'foreach' using 'closures': l = [1,2,"foo"];

l.each {
println "item ${it}";
};
What happens in this code is the following: first a list 'l' is created with elements 1, 2 and a string "foo". This list is iterated and for every item a string is printed to the screen which results in: item 1
item 2
item foo
(and it also illustrates autoboxing).

Two other nice things about Groovy are that

  • It is possible to compile Groovy scripts into regular Java code. This gives you classes that can be used in any regular Java application.
  • It is very easy to run Groovy scripts from the command-line. Simply typing $ groovy myscript.groovy launches the script. When you're using a *nix environment (includig cygwin) it is even easier: start the script file with #!/usr/bin/env groovy and you can start your script with $ ./myscript.groovy

Something else that's special about Groovy is that it is accepted as a JSR (Java Specification Request). That makes me wonder why Beanshell doesn't have a JSR, since it is much closer to Java than Groovy.

The reason why the article at onjava.com attracted my attention was that i had already read about Groovy a few months earlier. However that was in a complete different context: an open letter from Cedric Beust (who has an interesting blog btw) to the main developer of Groovy telling him he was making a mess of Groovy by not using a roadmap and always adding new features instead of working towards a 1.0 release. This gave me a rather negative feeling about Groovy and made me think Beanshell was a lot better then Groovy. But now that i have been playing a bit with Groovy i really like that too and think it is just 'different' from Beanshell. As an end user i'm very happy with what i have seen from Groovy until now. But ofcourse this negative criticism worries me a bit. I'd rather not invest my time in learning a language which will be dead in the future... Until now i think i'll just continue learning both the languages, Beanshell to write macro's in jEdit (although that's also possible with Groovy) and Groovy to write scripts for the command-line (and this is also possible with Beanshell)

So why is this item written in english?
Since a few months now i'm considering to write my tech-related articles in english to make them available to more people. For my 'regular' blog items i think Dutch is just fine and i will keep writing them in Dutch. I think i will just give it a try and see if i (and others) like it. (and you are free to correct my spelling mistakes, it can only have a positive influence :) )

A nice presentation of Groovy can be found here

Comments

Avatar

Gorik on 2005-05-10 19:37 reply

thx for this post :!: (and

thx for this post :!: (and the links)

I was curious about Groovy but never got around to actually reading anything about it ...

Groovy could prove very usefull on Solaris.

Avatar

TeRanEX on 2005-05-14 19:18 reply

tja daar heb ik al meerdere

tja daar heb ik al meerdere keren over gedacht maar meestal heb ik niet veel zin meer om heel het artikel ook nog eens te vertalen nadat ik het helemaal heb neergeschreven...

Avatar

highlander on 2005-06-17 20:32 reply

Your English is pretty good I

Your English is pretty good I think! And you're right, why losing everybody's precious time (damn, life is so short!) to learn zillion human languages when these English guys gave us this smooth and melodic Babel preventer? Don't listen to your lazy (or worse) brothers and keep communicating with the rest of this green planet.

Comment Atom Feed