Modified: 06/13/2008
File: HelloJava.java
public class HelloJava
{
public void greet()
{
System.out.println("Hello. My source code is Java.");
}
public static void main(String[] args)
{
HelloJava helloJava = new HelloJava();
helloJava.greet();
}
}
File: Hello.groovy
println "Hello groovy citizen."
File: org\example\GroovyHelloInPackage.groovy
package org.example println "Hello earth dweller."
File: HelloHasClass.groovy
class Greeter
{
void greet()
{
println("Good tidings.")
}
}
Greeter greeter = new Greeter()
greeter.greet()
Notice that the class name does not have to match the Groovy file name.
File: HelloWithMain.groovy
static main(String[] args)
{
println "Hello fellow traveler."
}
File: HelloILookLikeJava.groovy
public class HelloILookLikeJava
{
public void greet()
{
System.out.println("Hello, my source code looks like Java.");
}
public static void main(String[] args)
{
HelloILookLikeJava helloILookLikeJava = new HelloILookLikeJava();
helloILookLikeJava.greet();
}
}
groovy Hello.groovy
or
groovy Hello
groovy org/example/GroovyHelloInPackage.groovy
or
groovy org/example/GroovyHelloInPackage
groovy target/classes/basics/begin/Hello groovy target/classes/basics/begin/org/example/GroovyHelloInPackage groovy target/classes/basics/begin/HelloHasClass groovy target/classes/basics/begin/HelloWithMain groovy target/classes/basics/begin/HelloILookLikeJava
groovysh
groovyConsole
groovyc GroovyHello.groovy
groovyc org/example/GroovyHello.groovy
<taskdef name="groovyc"
classname="org.codehaus.groovy.ant.Groovyc"
classpathref="my.classpath"/>
...
<groovyc srcdir="${testSourceDirectory}" destdir="${testClassesDirectory}">
<classpath>
<pathelement path="${mainClassesDirectory}"/>
<pathelement path="${testClassesDirectory}"/>
<path refid="testPath"/>
</classpath>
<javac source="1.4" target="1.4" debug="on" />
</groovyc>
...
To run class files compiled from Groovy code, you will need to include the groovy-all-<version>.jar on the classpath.
With JDK 6.0:
java -cp .;E:\_dev\tools\groovy-1.6-beta-1\embeddable\* Hello
With JDK 5.0 or below:
java -cp .;E:\_dev\tools\groovy-1.6-beta-1\embeddable\groovy-all-1.6-beta-1.jar Hello
With JDK 6.0:
java -cp .;E:\_dev\tools\groovy-1.6-beta-1\embeddable\* org.example.GroovyHello
With JDK 5.0 or below:
java -cp .;E:\_dev\tools\groovy-1.6-beta-1\embeddable\groovy-all-1.6-beta-1.jar org.example.GroovyHello
TODO: If you provide a ...
java.io.* java.lang.* java.math.BigDecimal java.math.BigInteger java.net.* java.util.* groovy.lang.* groovy.util.*
prtinln "this is a statement"
prtinln("this is a statement")
prtinln "this is a statement"
prtinln()
obj.getClass().getName()
becomes
obj.class.name
bean.setName("Sam")
becomes
bean.name = "Sam"
Groovy has all of Java's keywords and some extra ones.