我有2個腳本,我試圖測試傳遞參數,並失敗。我檢查了GroovyScriptEngine的文檔,但似乎沒有處理我想要傳遞參數而不是屬性值對(在綁定中)的情況。如何將參數傳遞給Groovy 2.0腳本?
這裏是我的錯誤:
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;
}
不應該設置變量(args,[「input」,「world」])? –