The combination of Java and groovy scripting

Source: Internet
Author: User
Tags stub groovy script

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.