Embedded Groovy - loading scripts from main script
I'm trying to embed groovy into my application and have a problem with imports.
I wish to split scripts into several files and/or modules. Lets say I want to have some Utilities.groovy
with Utilities
class filled with static functions. Now I create a primary script file main.groovy
that looks like this:
imp开发者_如何学JAVAort static Utilities.*
Utilities.someMethod()
Then I try to run it with:
GroovyShell shell = new GroovyShell(initGroovyBinding());
shell.run("F:\\ull\path\\to\\main.groovy", new String[0]);
And I get an error: unable to resolve class Utilities
What am I doing wrong? Thanks in advance.
Looks like you are missing the package. Based on your example you are trying to import Utilities from the default package.
Solved my problem. GroovyShell does not handle file imports in any way. However, GroovyScriptEngine does.
GroovyScriptEngine does everything GroovyShell can do plus it handles all the class dependencies.
精彩评论