简体   繁体   中英

groovy file call in java

i want to do groovy file call in java

my source:

ex.groovy

def swingBuilder = new SwingBuilder()
swingBuilder.edt {
frame(title: 'ex', size: [200, 150], show: true) {
    borderLayout(vgap: 5)
    panel(constraints: BorderLayout.CENTER, border: emptyBorder(10)) {
        button "a"
        button "b"
    }
  }
}

ex1.java

class ex1{
public static void main(String[] args)throws Exception {
    File sourceFile = new File("mypath/ex.groovy");
    ClassLoader clo = jview.class.getClassLoader();
    GroovyClassLoader classLoader = new GroovyClassLoader(clo);
    Class groovy = classLoader.parseClass(sourceFile);
    GroovyObject groovyob = (GroovyObject)groovy.newInstance();
    groovyob.invokeMethod("run", null);
    }
}

how do i?

help please..

You could use GroovyShell (if required with Binding)

import groovy.lang.Binding;
import groovy.lang.GroovyShell;

public class ex1 {

  public static void main(String[] args) {
    Binding binding = new Binding();
    GroovyShell shell = new GroovyShell(binding);

    shell.evaluate("mypath/ex.groovy");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM