需要访问在groovy脚本文件中定义的变量
在使用GroovyShell
执行脚本之前
然而,我收到了错误:
groovy.lang.MissingPropertyException: No such property: _THREADS for class: Test1
//Test1.groovy
_THREADS=10;
println("Hello from the script test ")
//scriptRunner.groovy
File scriptFile = new File("Test1.groovy");
def sharedData = new Binding()
def shell = new GroovyShell(sharedData)
def script = shell.parse (scriptFile);
def thread = script.getProperty('_THREADS'); // getting ERROR --
// No such property: _THREADS for class: Test1
//-------
//now run the threads
script.run()
2条答案
按热度按时间wr98u20j1#
在获取属性/绑定之前,您必须运行脚本
尝试在groovy控制台中打开此代码的
AST Browser (Ctrl+T)
:您将看到大致如下所生成的类:
其中
_THREADS = 10
将属性分配给绑定但是,也可以将_thresses定义为一个函数,然后无需运行脚本即可访问它。
agxfikkp2#
得到了使用
@Field
修改符值可以在解析后访问的解决方案