从Java调用Groovy函数

joe*_*moe 27 java groovy

如何从Java调用Groovy脚本文件中定义的函数?

示例groovy脚本:

def hello_world() {
   println "Hello, world!"
}
Run Code Online (Sandbox Code Playgroud)

我查看了GroovyShell,GroovyClassLoader和GroovyScriptEngine.

tim*_*tes 40

假设您有一个名为的文件test.groovy,其中包含(如您的示例所示):

def hello_world() {
   println "Hello, world!"
}
Run Code Online (Sandbox Code Playgroud)

然后你可以创建一个Runner.java这样的文件:

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() ;
  }
}
Run Code Online (Sandbox Code Playgroud)

编译它:

$ 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.
Run Code Online (Sandbox Code Playgroud)

(注意:警告留给读者练习);-)

然后,您可以运行此Runner.class:

$ java -cp .:groovy-all-1.7.5.jar Runner
Hello, world!
Hello, world!
Hello, world!
Run Code Online (Sandbox Code Playgroud)

  • 我同意你的想法 (2认同)

ata*_*lor 20

最简单的方法是将脚本编译成java类文件,然后直接调用它.例:

// Script.groovy
def hello_world() {
    println "Hello, World!"
}

// Main.java
public class Main {
    public static void main(String[] args) {
        Script script = new Script();
        script.hello_world();
    }
}

$ groovyc Script.groovy
$ javac -classpath .:$GROOVY_HOME/embeddable/groovy-all-1.7.5.jar Main.java
$ java -classpath .:$GROOVY_HOME/embeddable/groovy-all-1.7.5.jar Main
Hello, World!
Run Code Online (Sandbox Code Playgroud)


too*_*kit 9

  1. 编译为ataylor建议
  2. 使用JSR-223作为解释这里
  3. 如果您使用的是Spring,请使用实现Java接口的groovy类,并使用以下代码注入代码:

<lang:groovy id="messenger" script-source="classpath:Messenger.groovy">
    <lang:property name="message" value="I Can Do The Frug" />
</lang:groovy>
Run Code Online (Sandbox Code Playgroud)

弹簧方法的一个优点是'可刷新的豆'的概念.也就是说,可以将Spring配置为监视脚本文件以进行修改,并在运行时替换.