title: Calling groovy method from script
body: <p>I'm writing a program in groovy, I want it to run a script with dynamically changing variables. The script is supposed to execute a method with its variables. This method is located in the program that runs the script.</p>
  
  <p>The script would contain something like this:</p>
  
  <pre><code>// 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 &gt; 9000) { output = "ERROR" }
  
  // the same thing maybe a few more times
  value = getValue(...
  </code></pre>
  
  <p>I want to keep it as simple as possible.</p>
  
  <p>I was working with these examples: <a href="http://groovy.codehaus.org/Embedding+Groovy" rel="nofollow">http://groovy.codehaus.org/Embedding+Groovy</a></p>
  
  <p>I already tried to use the GroovyScriptEngine, but it seems like the script only accepts Strings as input values. It would be great if I could hand over method pointers.</p>
  
  <p>Just trying to hand over an integer like this:</p>
  
  <pre><code>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")
  </code></pre>
  
  <p>With this script.groovy</p>
  
  <pre><code>output = ${input}
  </code></pre>
  
  <p>results in this error:</p>
  
  <pre><code>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;)
  </code></pre>
  
  <p>Receiving the value in the script as a String like "${input}" works just fine.</p>
  
  <p>I know it has to work somehow and I would appreciate any help or other suggestions!</p>