首页 > 代码库 > JAVA与groovy脚本的结合使用
JAVA与groovy脚本的结合使用
java执行groovy shell脚本
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.0-beta-1</version>
</dependency>
添加groovy jar包支持
====================================================
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 = http://www.mamicode.com/gs.evaluate("println ‘Hello Groovy !‘;abc=123;return var*10");//执行groovyshell脚本 System.out.println(value.equals(50)); System.out.println(binding.getVariable("abc").equals(123)); } } |
++++++++++++++++++++++++++++++++++++++++++++++++++++JAVA中动态运行groovy脚本
通过JAVA来加载如一个groovy脚本文件,然后调用该脚本中的方法
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脚本
package groovy class GroovyDemo { static int add(int x,int y) { return x+y; } static main(args) { int z = add(123,321); println("x+y="+z); } } |
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Groovy脚本引擎
通过设定CLASSPATH来初始化groovy脚本引擎,可以运行该path下的任何groovy脚本文件了
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脚本
package groovy; output = "Hello ${input}"; |
JAVA与groovy脚本的结合使用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。