Reputation: 216
I have a project with multiple groovy files, and I have several "tiny" classes that I want to put in 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?
Upvotes: 5
Views: 3676
Reputation: 10925
When Groovy is run as a script without compilation, then classes are resolved by matching names to a corresponding *.groovy
source files, so only classes where the class name matches the source filename can be found.
This is known problem marked as Not a Bug.
As a workaround you can compile classes first with groovyc
and then run using java
:
groovyc *
java -cp '/some/path/groovy-2.4.3/lib/groovy-2.4.3.jar;.' script
Foo
Bar
Baz
Upvotes: 7
Reputation: 15205
Well, you haven't really explained why you would want to do this, so perhaps the obvious answer won't help you, but you can just do this:
#! env groovy
class Foo
{
Foo() { println "Foo" }
}
class Bar
{
Bar() { println "Bar" }
}
class Baz
{
Baz() { println "Baz" }
}
new Foo()
new Bar()
new Baz()
Upvotes: 0