目录:
- 程序清单
- Groovy运行HelloGroovy
- Java运行HelloJava
- Java运行HelloGroovy
- 字节码
[一]、程序清单
Groovy代码:HelloGroovy.groovy
1 |
println "Hello Groovy welcome to www.micmiu.com" |
Java代码:HelloJava.java
1 2 3 4 5 6 7 8 9 10 11 |
public class HelloJava { /** * @param args */ public static void main(String[] args) { System.out.println("Hello Java welcome to www.micmiu.com"); } } |
[二]、Groovy运行HelloGroovy
按win+R键 输入cmd 回车进入dos控制台界面,执行如下命令:
1 2 3 4 5 |
D:\test\groovy>groovy HelloGroovy.groovy Hello Groovy welcome to www.micmiu.com D:\test\groovy>groovy HelloGroovy Hello Groovy welcome to www.micmiu.com |
[三]、Java运行HelloJava
在控制台界面中执行如下命令:
1 2 3 4 |
D:\test\groovy>javac HelloJava.java D:\test\groovy>java HelloJava Hello Java welcome to www.micmiu.com |
[四]、Java运行HelloGroovy
用groovyc 编译 HelloGroovy.groovy源文件:
1 2 3 4 5 6 7 8 9 10 11 12 |
D:\test\groovy>groovyc HelloGroovy.groovy D:\test\groovy>tree /F 卷 work 的文件夹 PATH 列表 卷序列号为 2AF7-9BD9 D:. HelloGroovy.class HelloGroovy.groovy HelloJava.class HelloJava.java 没有子文件夹 |
运用java 执行编译后的类:HelloGroovy.class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
D:\test\groovy>java HelloGroovy Exception in thread "main" java.lang.NoClassDefFoundError: groovy/lang/Script at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632) at java.lang.ClassLoader.defineClass(ClassLoader.java:616) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14 1) at java.net.URLClassLoader.defineClass(URLClassLoader.java:283) at java.net.URLClassLoader.access$000(URLClassLoader.java:58) at java.net.URLClassLoader$1.run(URLClassLoader.java:197) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:248) Caused by: java.lang.ClassNotFoundException: groovy.lang.Script at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:248) ... 12 more Could not find the main class: HelloGroovy. Program will exit. |
这样直接运行会出错,是因为没有把 Groovy 相关的jar包添加到Java的CLASSPATH中,执行如下命令即可正确运行:
1 2 |
D:\test\groovy>java -cp .;%GROOVY_HOME%\embeddable\groovy-all-2.0.1.jar HelloGroovy Hello Groovy welcome to www.micmiu.com |
这样Java就可以正常的运行Groovy的程序了。
[五]、字节码
运用javap 查看编译后类文件的字节码信息:
HelloJava.class:
1 2 3 4 5 6 |
D:\test\groovy>javap HelloJava Compiled from "HelloJava.java" public class HelloJava extends java.lang.Object{ public HelloJava(); public static void main(java.lang.String[]); } |
HelloGroovy.class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
D:\test\groovy>javap HelloGroovy Compiled from "HelloGroovy.groovy" public class HelloGroovy extends groovy.lang.Script{ public static transient boolean __$stMC; public static long __timeStamp; public static long __timeStamp__239_neverHappen1343966577689; public HelloGroovy(); public HelloGroovy(groovy.lang.Binding); public static void main(java.lang.String[]); public java.lang.Object run(); public java.lang.Object this$dist$invoke$3(java.lang.String, java.lang.Objec t); public void this$dist$set$3(java.lang.String, java.lang.Object); public java.lang.Object this$dist$get$3(java.lang.String); protected groovy.lang.MetaClass $getStaticMetaClass(); public static void __$swapInit(); static {}; public java.lang.Object super$3$getProperty(java.lang.String); public java.lang.String super$1$toString(); public void super$3$setProperty(java.lang.String, java.lang.Object); public void super$1$notify(); public void super$3$println(); public void super$1$notifyAll(); public void super$3$print(java.lang.Object); public void super$3$printf(java.lang.String, java.lang.Object[]); public java.lang.Object super$1$clone(); public java.lang.Object super$3$evaluate(java.lang.String); public void super$1$wait(); public groovy.lang.MetaClass super$2$getMetaClass(); public void super$1$wait(long, int); public void super$2$setMetaClass(groovy.lang.MetaClass); public java.lang.Class super$1$getClass(); public groovy.lang.Binding super$3$getBinding(); public void super$1$finalize(); public void super$3$printf(java.lang.String, java.lang.Object); public void super$3$setBinding(groovy.lang.Binding); public void super$1$wait(long); public void super$3$run(java.io.File, java.lang.String[]); public java.lang.Object super$3$evaluate(java.io.File); public void super$3$println(java.lang.Object); public boolean super$1$equals(java.lang.Object); public java.lang.Object super$3$invokeMethod(java.lang.String, java.lang.Obj ect); public int super$1$hashCode(); static java.lang.Class class$(java.lang.String); } |
从字节码中可以看出主要的main方法是一样的:
1 |
public static void main(java.lang.String[]); |
只是区别在:extends java.lang.Object
或提供的类extends groovy.lang.Script
,而这些区别对于一位coder来说是透明的,无需显式注明。
到此本文讲解基本结束。
原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴[ http://www.micmiu.com/ ]
本文链接地址: http://www.micmiu.com/lang/groovy/groovy-tutorial-vsjava/
0 条评论。