从Java调用Groovy函数 [英] Calling a Groovy function from Java
本文介绍了从Java调用Groovy函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
示例groovy脚本:
def hello_world(){
printlnHello,world!
$ b我已经看过GroovyShell, >
解决方案假设您有一个名为 test.groovy
的文件,其中包含你的例子):
def hello_world(){
printlnHello,world!
}
然后您可以创建一个文件 Runner.java
像这样:
import groovy.lang.GroovyShell;
导入groovy.lang.GroovyClassLoader;
导入groovy.util.GroovyScriptEngine;
import java.io.File;
$ b $ class Runner {
static void runWithGroovyShell()throws Exception {
new GroovyShell()。parse(new File(test.groovy)).invokeMethod(hello_world , 空值 ) ;
}
static void runWithGroovyClassLoader()抛出异常{
//声明一个类符合java接口类会摆脱
//很多这里的反射
class scriptClass = new GroovyClassLoader()。parseClass(new File(test.groovy));
Object scriptInstance = scriptClass.newInstance();
scriptClass.getDeclaredMethod(hello_world,new Class [] {}).invoke(scriptInstance,new Object [] {});
}
static void runWithGroovyScriptEngine()抛出异常{
//声明一个符合java接口类的类将抛弃
//很多这里的反映
Class scriptClass = new GroovyScriptEngine(。).loadScriptByName(test.groovy);
Object scriptInstance = scriptClass.newInstance();
scriptClass.getDeclaredMethod(hello_world,new Class [] {}).invoke(scriptInstance,new Object [] {});
public static void main(String [] args)throws Exception {
runWithGroovyShell();
runWithGroovyClassLoader();
runWithGroovyScriptEngine();
$ / code>
使用以下内容进行编译:
$ javac -cp groovy-all-1.7.5.jar Runner.java
注意:Runner.java使用未经检查或不安全的操作。
注意:使用-Xlint重新编译:取消选中以获取详细信息。
(注意:这些警告仅供读者参考); - )
然后,你可以运行这个Runner.class:
$ java -cp 。:groovy-all-1.7.5.jar亚军
你好,世界!
你好,世界!
你好,世界!
How do you call a function defined in a Groovy script file from Java?
Example groovy script:
def hello_world() {
println "Hello, world!"
}
I've looked at the GroovyShell, GroovyClassLoader, and GroovyScriptEngine.
解决方案 Assuming you have a file called test.groovy
, which contains (as in your example):
def hello_world() {
println "Hello, world!"
}
Then you can create a file Runner.java
like this:
import groovy.lang.GroovyShell ;
import groovy.lang.GroovyClassLoader ;
import groovy.util.GroovyScriptEngine ;
import java.io.File ;
class Runner {
static void runWithGroovyShell() throws Exception {
new GroovyShell().parse( new File( "test.groovy" ) ).invokeMethod( "hello_world", null ) ;
}
static void runWithGroovyClassLoader() throws Exception {
// Declaring a class to conform to a java interface class would get rid of
// a lot of the reflection here
Class scriptClass = new GroovyClassLoader().parseClass( new File( "test.groovy" ) ) ;
Object scriptInstance = scriptClass.newInstance() ;
scriptClass.getDeclaredMethod( "hello_world", new Class[] {} ).invoke( scriptInstance, new Object[] {} ) ;
}
static void runWithGroovyScriptEngine() throws Exception {
// Declaring a class to conform to a java interface class would get rid of
// a lot of the reflection here
Class scriptClass = new GroovyScriptEngine( "." ).loadScriptByName( "test.groovy" ) ;
Object scriptInstance = scriptClass.newInstance() ;
scriptClass.getDeclaredMethod( "hello_world", new Class[] {} ).invoke( scriptInstance, new Object[] {} ) ;
}
public static void main( String[] args ) throws Exception {
runWithGroovyShell() ;
runWithGroovyClassLoader() ;
runWithGroovyScriptEngine() ;
}
}
compile it with:
$ javac -cp groovy-all-1.7.5.jar Runner.java
Note: Runner.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
(Note: The warnings are left as an exercise to the reader) ;-)
Then, you can run this Runner.class with:
$ java -cp .:groovy-all-1.7.5.jar Runner
Hello, world!
Hello, world!
Hello, world!
这篇关于从Java调用Groovy函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文