您的位置:首页 > 编程语言 > Java开发

Access Java API in Groovy Script

2014-03-17 18:20 295 查看
$ cat Hello.java

package test;

public class Hello {

public int myadd(int x, int y) {

return 10 * x + y;

}

}

$ cat myapp.groovy

import test.Hello

def hello = new Hello()

println hello.myadd(13, 5)

$ 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:

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

If your groovy script don't access other Java class, you can simply run "java -jar groovy-all-2.2.1.jar myapp.groovy".
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: