1.maven引入groovy jar包java
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.1.9</version>
</dependency>maven
2.調用 lua
package com.test;code
import java.util.ArrayList;
import java.util.List;對象
import groovy.lang.GroovyShell;io
public class TestGroovy {
public static void main(String[] args) {
// TODO Auto-generated method stub
long l1 = System.currentTimeMillis();
GroovyShell groovyShell = new GroovyShell();
System.out.println(System.currentTimeMillis()-l1);//new一個groovy對象時間好像有點久 100多毫秒
long l2 = System.currentTimeMillis();
TestGroovy t = new TestGroovy();
System.out.println(System.currentTimeMillis()-l2);
Object resultObj;
int x = 1;
groovyShell.setVariable("x", 3);//註冊變量值
List<String> list = new ArrayList<String>();
groovyShell.setVariable("list", list);//註冊變量值
resultObj = groovyShell.evaluate("import com.test.TestGroovy;TestGroovy.sys(''+x);list.add('xxxx');x=4; x<1;"); //groovy執行 、還能夠import java類進行調用 groovy默認返回最後一行的執行結果
System.out.println(resultObj);
System.out.println(x);//groovy沒有改變x的值
System.out.println(list);//groovy 改變了list的值
resultObj = groovyShell.evaluate("import com.test.TestGroovy;TestGroovy.sys(''+x)"); //以前設置的x值還在 x=4;
}class
public static void sys(String s){
System.out.println(s);
}test
}import