从脚本调用groovy方法

从脚本调用groovy方法,groovy,scripting,Groovy,Scripting,我正在用groovy编写一个程序,我希望它运行一个带有动态变化变量的脚本。脚本应该执行一个带有变量的方法。此方法位于运行脚本的程序中 脚本将包含如下内容: // getAttribute is a method in my main program, the program which also runs the script value = getValue("java.lang:type=OperatingSystem", "FreePhysicalMemorySize") // do s

我正在用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(...
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")
我想让它尽可能简单

我正在使用这些示例:

我已经尝试过使用GroovyScript引擎,但是脚本似乎只接受字符串作为输入值。如果我能交出方法指针那就太好了

只是想交一个整数,如下所示:

// 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(...
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: [script$_run_closure1@fcf50]
Possible solutions: is(java.lang.Object), run(), run(), any(), any(groovy.lang.Closure), use([Ljava.lang.Object;)
将脚本中的值作为“${input}”之类的字符串接收就可以了

我知道它必须以某种方式工作,我将感谢任何帮助或其他建议

output = ${input}
这不是有效的groovy吗

尝试: