Java Execution Groovy Shell script
<dependency> <groupId>org.codehaus.groovy</groupId> <artifactid>groovy-all</ Artifactid> <version>2.4.0-beta-1</version></dependency>
Add Groovy Jar Package Support ====================================================
Package Groovy;import Groovy.lang.binding;import Groovy.lang.groovyshell;public class Java2groovyshell {public static void Main (string[] args) {//TODO auto-generated method Stub binding binding = new binding (); Binding.setvariable ("Var", 5); Groovyshell GS = new Groovyshell (binding); Object value = gs.evaluate ("println ' Hello Groovy! '; Abc=123;return var*10 ");//execute Groovyshell Script System.out.println (Value.equals (50)); System.out.println (Binding.getvariable ("abc"). Equals (123)); }} |
==================================================== script that runs groovy directly
++++++++++++++++++++++++++++++++++++++++++++++++++++java run Groovy script in Java to load a groovy script file, and then invoke the method in the script
Package Groovy;import Groovy.lang.groovyclassloader;import Groovy.lang.groovyobject;import java.io.File;public Class Java2groovydynamic {public static void main (string[] args) throws Exception {//TODO auto-generated method stub ClassLoader parent = Classloader.getsystemclassloader (); Groovyclassloader loader = new Groovyclassloader (parent); Class Groovyclass = Loader.parseclass (New File ("C:\\normandy_workspace\\groovy\\src\\groovy\\groovydemo.groovy")); Groovyobject Groovyobject = (groovyobject) groovyclass.newinstance (); object[] param = {123,321}; int res = (int) groovyobject.invokemethod ("Add", param); System.out.println ("res=" +res); }} |
Groovy Script
Package Groovyclass Groovydemo {static int add (int x,int y) {return x+y;} Static main (args) {int z = Add (123,321); println ("x+y=" +z); }} |
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
The Groovy scripting engine initializes the groovy scripting engine by setting Classpath to run any groovy script file under that path.
Package Groovy;import Groovy.lang.binding;import Groovy.util.groovyscriptengine;public class Java2GroovyEngine { public static void Main (string[] args) throws Exception {//TODO auto-generated method stub String Path = "C:\\normandy _workspace\\groovy\\src\\groovy\\ "; Groovyscriptengine GSE = new Groovyscriptengine (path); Binding binding = new binding (); Binding.setvariable ("Input", "Groovy"); Gse.run ("Test.groovy", binding); System.out.println (binding.getvariable ("output")); }} |
Groovy Script
Package Groovy;output = "Hello ${input}"; |
The combination of Java and groovy scripting