{"id":8695,"date":"2012-09-25T23:08:40","date_gmt":"2012-09-25T17:38:40","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=8695"},"modified":"2014-12-17T09:12:29","modified_gmt":"2014-12-17T03:42:29","slug":"executing-groovy-scripts-at-runtime","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/executing-groovy-scripts-at-runtime\/","title":{"rendered":"Executing Groovy scripts at runtime"},"content":{"rendered":"<p>A very useful Groovy class <strong>GroovyScriptEngine<\/strong> can be used to execute Groovy scripts at runtime.<br \/>\n<br \/>\nFor understanding purpose, lets start with an example. We have a script named as <strong>Area.groovy<\/strong> that calculates the area of circle.<\/p>\n<p>[java]<\/p>\n<p>\/\/ Area.groovy<\/p>\n<p>Float r = &quot;${radius}&quot;.toFloat()  \/\/radius will be passed as an argument to this script.<\/p>\n<p>return &quot;Area of the circle : &quot; + Math.PI*r*r;<\/p>\n<p>[\/java]<\/p>\n<p>Now if we want to execute the <strong>Area.groovy<\/strong> script at runtime, then the solution is to use <strong>GroovyScriptEngine<\/strong> class. For using <strong>GroovyScriptEngine<\/strong> class, there is need to pass the directory path to tell where the groovy script is saved. e.g.<\/p>\n<p>[java]<br \/>\nGroovyScriptEngine gse = new GroovyScriptEngine(&lt;Directory_Path&gt;);<br \/>\n[\/java]<\/p>\n<p>If there is need to pass any argument in the script, then we can use <strong>Binding<\/strong> class to bind the argument with the script.<\/p>\n<p>e.g.<\/p>\n<p>[java]<br \/>\nimport groovy.lang.Binding;<br \/>\nimport groovy.util.GroovyScriptEngine;<\/p>\n<p>GroovyScriptEngine gse = new GroovyScriptEngine(&quot;\/home\/divya\/Desktop&quot;);<\/p>\n<p>Binding binding = new Binding();<\/p>\n<p>binding.setVariable(&quot;radius&quot;, 5); \/\/ Argument to be passed is &quot;radius&quot; with value 5<\/p>\n<p>String areaOfCircle = gse.run(&quot;Area.groovy&quot;, binding);<\/p>\n<p>println areaOfCircle<\/p>\n<p>\/\/ Output:  Area of the circle : 78.53981633974483<br \/>\n[\/java]<\/p>\n<p>The run method is used to execute the script.<br \/>\n<br \/>\nHope it would help!!<br \/>\n<br \/>\n<a href=\"http:\/\/www.tothenew.com\/blog\/author\/divya\/\">Divya Setia<\/a><br \/>\n<a href=\"mailto:divya@intelligrape.com\">divya@intelligrape.com<\/a><br \/>\n<a href=\"https:\/\/twitter.com\/divs157\">https:\/\/twitter.com\/divs157<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A very useful Groovy class GroovyScriptEngine can be used to execute Groovy scripts at runtime. For understanding purpose, lets start with an example. We have a script named as Area.groovy that calculates the area of circle. [java] \/\/ Area.groovy Float r = &quot;${radius}&quot;.toFloat() \/\/radius will be passed as an argument to this script. return &quot;Area [&hellip;]<\/p>\n","protected":false},"author":29,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":19},"categories":[7],"tags":[1083,1082,474],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8695"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/29"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=8695"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8695\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=8695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=8695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=8695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}