Monday, September 05, 2011

Groovy and its bad moods

I had not picked up Groovy in a while. So I downloaded Groovy 1.8.1 and off I went with HelloWorld.
class Greet {
def name
Greet(who) { name = who[0].toUpperCase() +
who[1..-1] }
def salute() { println "Hello $name!" }
}

g = new Greet('world') // create object
g.salute()

Then I compiled it in the GroovyConsole and I got the following error:
Invalid duplicate class definition of class Greet : The source Greet.groovy contains at least two definitions of the class Greet.


What da!?
It turns out that the Groovy compiler tries to put the code that is outside the class into a .class file called Greet.class Ooops! So the quick fix is to save the file as a different name such as Foo.groovy. Then after the Groovy compiler runs, you will end you with Foo.class and Greet.class

Now you know.