groovy与javah互相调用
时间:2014-03-12 16:46:31
收藏:0
阅读:432
暂时只找到这几种方法。groovy代码方便灵活。常用来写一些工具。一般都是java加入groovy代码
第一种 java中加入 groovy代码就是
Toy.groovy
1
2
3
4
5
6
7
8
9
10
11 |
class Toy{ def p(arg) { arg.each{ println it } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13 |
GroovyClassLoader gcl = new
GroovyClassLoader(); Class greetingClass = null ; try { greetingClass = gcl.parseClass( new
File( "Toy.groovy" )); } catch
(IOException e) { e.printStackTrace(); } GroovyObject hello = (GroovyObject) new
Toy(); String[] a={ "a" , "b" , "c" }; Object[] agr={a}; hello.invokeMethod( "p" ,agr) ; |
另一种就是将groovy的代码打包成jar ,和java打包类似 ,就可以当成java代码那样打包,然后加到java 项目的classpath中这样执行
1
2
3
4
5
6 |
GroovyObject hello = (GroovyObject) new
Toy(); String[] a={ "a" , "b" , "c" }; Object[] agr={a}; hello.invokeMethod( "p" ,agr) ; |
groovy中引入java代码同上类似。就是将java代码打包后加入到 java 项目的classpath中
例如 app2是一个javal类
1
2
3
4
5 |
public class App2 { public
void p1() { System.out.println( "hello world" ); } } |
在groovy中引入
1
2 |
def te = new
App2() te.p1() |
注意使用groovy时把groovy的jar包引入java项目中
groovy与javah互相调用,布布扣,bubuko.com
原文:http://www.cnblogs.com/or2-/p/3596470.html
评论(0)