Search code examples
scriptinggroovyimportembedding

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:

import 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.


Solution

  • 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.