Access Java API in Groovy Script

$ cat Hello.java java

package test;app

public class Hello {ui

public int myadd(int x, int y) {code

return 10 * x + y;

}ip

}get

$ cat myapp.groovy class

import test.Hellotest

def hello = new Hello()import

println hello.myadd(13, 5)im

$ javac Hello.java

$ mkdir test;mv Hello.class test

$ tree

.

├── groovy-all-2.2.1.jar

├── Hello.java

├── myapp.groovy

└── test

└── Hello.class

$ java -cp .:groovy-all-2.2.1.jar groovy.ui.GroovyMain myapp.groovy

135

Note:

  1. You can't use groovy-2.2.1.jar here, or you will get a NoClassDefFoundError;

  2. If your groovy script don't access other Java class, you can simply run "java -jar groovy-all-2.2.1.jar myapp.groovy".

相關文章
相關標籤/搜索