开发者

Embedded Groovy - loading scripts from main script

开发者 https://www.devze.com 2023-01-29 19:46 出处:网络
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 Utilitie

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消