且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从Java调用Groovy函数

更新时间:2023-12-05 13:34:22

假设您有一个名为 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!