我有 2 个脚本,我试图在其中测试传递一个参数,但它失败了。我检查了 GroovyScriptEngine 的文档,但它似乎没有处理我想要传递 arg 而不是属性值对(在绑定中)的情况。
这是我得到的错误:
C:\AeroFS\Work\Groovy_Scripts>groovy scriptengineexample.groovy
hello, world
Caught: groovy.lang.MissingPropertyException: No such property: args
for class: hello
groovy.lang.MissingPropertyException: No such property: args for
class: hello
at hello.run(hello.groovy:4)
at Test.main(scriptengineexample.groovy:14)
这是我的脚本:
import groovy.lang.Binding;
import groovy.util.GroovyScriptEngine;
import groovy.util.ResourceException ;
import groovy.util.ScriptException ;
import java.io.IOException ;
public class Test {
public static void main( String[] args ) throws IOException,
ResourceException, ScriptException {
GroovyScriptEngine gse = new GroovyScriptEngine( [ '.' ] as String[] )
Binding binding = new Binding();
binding.setVariable("input", "world");
gse.run("hello.groovy", binding);
System.out.println( "Output: " + binding.getVariable("output") );
}
}
和这个:
//hello.groovy
println "hello.groovy"
for (arg in this.args ) {
println "Argument:" + arg;
}