Close

Groovy - Class File Vs Script File

[Updated: Feb 24, 2019, Created: Feb 24, 2019]

In Groovy, one source file may contain one or more class definitions, but if a file contains any code outside of a class, it is considered a script.

Example

Let's create a groovy file which contains only a class:

src/Person.groovy

class Person {
    def name;

    @Override
    String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                '}';
    }
}

The file name and class name do not need to be same. But on compiling the file, Person.class will be generated.

Let's compile above class and analyze the class file via javap:

C:\groovy-class-vs-script\src>groovyc Person.groovy

C:\groovy-class-vs-script\src>javap Person.class
Compiled from "Person.groovy"
public class Person implements groovy.lang.GroovyObject {
public static transient boolean __$stMC;
public Person();
public java.lang.String toString();
protected groovy.lang.MetaClass $getStaticMetaClass();
public groovy.lang.MetaClass getMetaClass();
public void setMetaClass(groovy.lang.MetaClass);
public java.lang.Object invokeMethod(java.lang.String, java.lang.Object);
public java.lang.Object getProperty(java.lang.String);
public void setProperty(java.lang.String, java.lang.Object);
public java.lang.Object getName();
public void setName(java.lang.Object);
public java.lang.String super$1$toString();
}

As seen above the file contains just class definition plus some groovy hooks. It does not have a main method and a run method like a groovy script has ( tutorial).

Now let's create a groovy script which will be using above Person class:

src/test.groovy

def person = new Person();
person.name = "Tina"
println person

Output

Running test.groovy from IDE (I used Intellij) or running from command line as groovy test will produce following output (the Person.class does not need to be present, only if the script name is same as the class name):

Person{name='Tina'}

Creating multiple classes in a file

src/multiple-classes.groovy

class A {
    void methodA() {
        println "in methodA"
    }
}

class B {
    void methodB() {
        println "in methodB"
    }
}

class C {
    void methodC() {
        println "in methodC"
    }
}

If we compile above file via command groovyc multiple-classes.groovy, that will generate three files: A.class, B.class and C.class.

Let's create a groovy script which will use above classes:

src/multiple-classes-test.groovy

new A().methodA()
new B().methodB()
new C().methodC()

output

Running above script from IDE produces following output:

in methodA
in methodB
in methodC

Note that if we use groovy multiple-classes-test command to run above script, the three classes (A.class, B.class and C.class) must be present, if compiled classes are located somewhere else, then we have to use:
groovy --classpath=<path> multiple-classes-test

Example Project

Dependencies and Technologies Used:

  • Groovy 2.5.6
  • JDK 11
Groovy - Class file Vs Script File Select All Download
  • groovy-class-vs-script
    • src
      • Person.groovy

    See Also