Multiple Groovy classes in one file when run as a script

I have a project with several groovy files, and I have several “tiny” classes that I want to put into a single file.

Basically, here is what I want:

Foo.groovy:

class Foo
{
    Foo() { println "Foo" }
}

Bar.groovy:

class Bar
{
    Bar() { println "Bar" }
}

class Baz
{
    Baz() { println "Baz" }
}

script.groovy:

#!/groovy/current/bin/groovy

new Foo()
new Bar()
new Baz()

And then:

$ groovy ./script.groovy 
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/home/tmp/script.groovy: 5: unable to resolve class Baz 
 @ line 5, column 1.
   new Baz()
   ^

1 error

Any idea?

+4
source share
2 answers

When Groovy is run as a script without compilation, the classes are resolved by matching the names with the corresponding source files *.groovy, so you can only find classes in which the class name matches the original file name.

This is a known issue marked as "Not a bug."

groovyc, java:

groovyc *
java -cp '/some/path/groovy-2.4.3/lib/groovy-2.4.3.jar;.' script
Foo
Bar
Baz
+6

, , , , , , :

#! env groovy
class Foo
{
Foo() { println "Foo" }
}

class Bar
{
Bar() { println "Bar" }
}

class Baz
{
Baz() { println "Baz" }
}

new Foo()
new Bar()
new Baz()
-1

All Articles