Reputation: 2610
Is it possible to create class definitions within a groovy script?
I have a simple script example
class HelloWorld {
def name
def greet() {
"Hello ${name}"
}
}
def helloWorld = new HelloWorld()
helloWorld.name = "Groovy"
println helloWorld.greet()
but I get error like this
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 1: Class definition not expected here. Please define the class at an appropriate place or perhaps try using a block/Closure instead. at line: 1 column: 1. File: Script1.groovy @ line 1, column 1.
class HelloWorld {
^
1 error
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:309) ~[groovy-all-2.4.3.jar:2.4.3]
at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:943) ~[groovy-all-2.4.3.jar:2.4.3]
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:590) ~[groovy-all-2.4.3.jar:2.4.3]
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:566) ~[groovy-all-2.4.3.jar:2.4.3]
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:543) ~[groovy-all-2.4.3.jar:2.4.3]
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:297) ~[groovy-all-2.4.3.jar:2.4.3]
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:267) ~[groovy-all-2.4.3.jar:2.4.3]
at groovy.lang.GroovyShell.parseClass(GroovyShell.java:692) ~[groovy-all-2.4.3.jar:2.4.3]
at groovy.lang.GroovyShell.parse(GroovyShell.java:704) ~[groovy-all-2.4.3.jar:2.4.3]
at groovy.lang.GroovyShell.parse(GroovyShell.java:740) ~[groovy-all-2.4.3.jar:2.4.3]
at groovy.lang.GroovyShell.parse(GroovyShell.java:731) ~[groovy-all-2.4.3.jar:2.4.3
Upvotes: 1
Views: 8909
Reputation: 96444
Yes, in general it's normal to create class definitions within a script.
I'm not able to reproduce your error, and you don't say how you're running this code. I'm guessing you're trying to use a Groovy script to configure some product (Mule has this feature, for instance). Your problem would seem to be specific to the thing you're trying to configure.
If I try to run the script in groovysh I get "Unknown property" when it tries to define the helloWorld variable using def
.
In groovyconsole it works fine, as Dónal says.
If I take your code and put it in a file called HelloWorld.groovy and run that from the command line:
groovy HelloWorld.groovy
then I get
C:\Users\ndh\HelloWorld.groovy: 1: Invalid duplicate class definition of class HelloWorld : The source C:\Users\ndh\HelloWorld.groovy contains at least two definitions of the class HelloWorld. One of the classes is an explicit generated class using the class statement, the other is a class generated from the script body based on the file name. Solutions are to change the file name or to change the class name. @ line 1, column 1. class HelloWorld { ^
1 error
Changing the name of the file to notHelloWorld.groovy works:
c:\Users\ndh>groovy notHelloWorld.groovy
Hello Groovy
but this is not your problem because the stacktrace shows your filename to be Script1.
When I write a script I put the main logic at the top and put helper functions and class definitions toward the bottom. That's just how I organize the code for readability, though.
Upvotes: 1