At first, I wrote a simple hello world program with groovy using the Swing based Groovy Shell. It is very nice to find that a simple println will replace the whole System.out.println in java. It's like an advanced version of java. All java codes are valid groovy code, but groovy supports more. Like dynamic typing, closures, enhanced List and Map control are some of the main enhancements.
I also had read that I can use the groovy classes from my java program. Groovy does not require you to put your classes in saperate files, however for using with java, you must. I wrote the following program DemoGroovy.groovy and access the class from java class Main.java
************************************************************************************
//DemoGroovy.groovy
public class DemoGroovy{
public void say(){
println "Its Groovy!!"
}
}
************************************************************************************
//Main.java
public class Main{
public static void main(String [] args){
new DemoGroovy().say();
}
}
***********************************************************************************
The compilation was fine, but when I did java Main, there was a ClassNotFoundException thrown by the java class loader. I figured that I need to include all the groovy jar files to the CLASSPATH.
this would have been very tedious. So I decided to write a java program to generate the classpath as shown below.
*******************************************************************************************
//JavaG.java
import java.io.*;
public class JavaG{
public static void main(String [] args)throws Exception{
File file=new File("/opt/groovy-1.5.7/lib/");
StringBuilder path=new StringBuilder(".:");
for(String f:file.list()){
path.append("/opt/groovy-1.5.7/lib/"+f+":");
}
String p=path.substring(0,path.length()-1);
String command=p+" ";
System.out.println(command);
}
}
*******************************************************************************************
I compiled the class and copied the class file to /usr/bin. Then I wrote this Shell script at /usr/bin/groovypath show below
********************************************************************
#/usr/bin/groovypath
#!/bin/bash
java -cp /usr/bin JavaG
********************************************************************
After doing this, I ran my Main java program using this:
java -cp `groovypath` Main
and presto! it displays "It's Groovy!!", right as expected.
I then wrote the following program to have a better idea. This program is to print the files and directories present in the directories specified as command line arguments. If the argument is a file, it just displays the filename.
//DirectoryList.groovy
public class DirectoryList{
public static void main(args){
def listDir=
{File f->
if(f.isDirectory()){
f.list().each{
if(new File(f.toString()+"/"+it).isDirectory()){
println "Directory: "+f.toString()+"/"+it;
}
else{
println "File: "+f.toString()+"/"+it;
}
};
}
else{
println "File: "+f;
}
};
args.each{
File file=new File(it);
listDir(file);
}
}
}
And here is a sample output:
***************************************************************************************
#java -cp `groovypath` DirectoryList /opt/ .|sort
Directory: /opt/eboard-extras-1pl2
Directory: /opt/foobillard-2.9
Directory: /opt/fop-0.95
Directory: /opt/groovy-1.5.7
Directory: /opt/k3b-1.0.5
Directory: /opt/PyOpenGL-3.0.0b6
Directory: /opt/SecondLife-i686-1.21.6.99587
Directory: /opt/smplayer-0.6.5.1
Directory: /opt/XPontus
File: ./ArgDemo.class
File: ./ArgDemo.groovy
File: ./ArgDemo.groovy~
File: ./ArgDemo$_main_closure1.class
File: ./DemoGroovy.class
File: ./DemoGroovy.groovy
File: ./DemoGroovy.groovy~
File: ./DirectoryList.class
File: ./DirectoryList.groovy
File: ./DirectoryList.groovy~
File: ./DirectoryList$_main_closure1.class
File: ./DirectoryList$_main_closure1_closure3.class
File: ./DirectoryList$_main_closure1_closure4.class
File: ./DirectoryList$_main_closure2.class
File: ./JavaG.class
File: ./JavaG.java
File: ./JavaG.java~
File: ./Main.class
File: ./Main.java
File: /opt/AutoScan-Network-Linux-1.32.bin
File: /opt/Connect.jar
File: /opt/connect.sh
File: /opt/connect.sh~
*****************************************************************************************
That's just wonderful!
