Reputation: 4220
I'm working on a Java project which loads in Groovy scripts. Everything works as expected, except for groovy scripts which reference other groovy scripts, in this case, a utility class. Whenever a script is executed which includes said utility class, the plugin loader throws an exception "groovy.lang.MissingPropertyException: No such property: Util.
Before I suspect that something is wrong with the Java code, I wanted to first understand how Groovy "finds" other classes. If I'm running this project in eclipse, and the entire project directory which includes all these groovy scripts is in the classpath should all the groovy scripts be able to "find" each other?
Or any other thoughts?
Upvotes: 1
Views: 3232
Reputation: 20683
Groovy has a distinction between scripts and classes. If Groovy file contains some code outside class definition, then it is considered a script and it is compiled into a class located in default package (regardless of what is set in package
declaration).
I don't know how you call Groovy scripts, but if you use GroovyShell
class for that, then GroovyShell
has a constructor with a parameter of ClassLoader
type and you can pass your current class loader to that or your custom class loader that contains your other compiled Groovy scripts. Or if you use GroovyScriptEngine
you can pass a referenece to a directory where your scripts are and engine will compile them for you and make them available for other scripts in that directory.
More information on embedding Groovy into Java can be found here: http://groovy.codehaus.org/Embedding+Groovy.
But in general I need to know more about your setup (folder structure, build tool, do you compile groovy code during build process, how do you run groovy code from java, how do you call groovy script from groovy, how do you organize your classpath etc.) to give you some more precise advice.
Upvotes: 1