2
我正在寫在常規程序中調用Groovy方法,我希望它具有動態變化的變量運行的腳本。該腳本應該用其變量執行一個方法。該方法位於運行腳本的程序中。從腳本
該腳本將包含這樣的事情:
// getAttribute is a method in my main program, the program which also runs the script
value = getValue("java.lang:type=OperatingSystem", "FreePhysicalMemorySize")
// do something with the value
if (value > 9000) { output = "ERROR" }
// the same thing maybe a few more times
value = getValue(...
我想保持儘可能簡單。
我與這些例子的工作:http://groovy.codehaus.org/Embedding+Groovy
我已經嘗試使用的GroovyScriptEngine,但它似乎是腳本只接受字符串作爲輸入值。這將是巨大的,如果我能交出方法指針。
只是想交出這樣一個整數:
def roots = 'PATH\\script.groovy'
def groscreng = new GroovyScriptEngine(roots)
def binding = new Binding()
def input = 42
binding.setVariable("input", input)
groscreng.run("script.groovy", binding)
println binding.getVariable("output")
有了這個script.groovy
output = ${input}
導致此錯誤:
Caught: groovy.lang.MissingMethodException: No signature of method: script.$() is applicable for argument types: (script$_run_closure1) values: [[email protected]]
Possible solutions: is(java.lang.Object), run(), run(), any(), any(groovy.lang.Closure), use([Ljava.lang.Object;)
接受的價值腳本像「$ {}輸入」字符串工作得很好。
我知道它以某種方式工作,我希望得到任何幫助或其他建議!
謝謝!這有效,不能相信我錯過了... – Peddastic