Pages

Saturday, December 13, 2008

I started Groovy-ing

I recently started learning groovy. It is really fun. I am reading this book "Working with Groovy" from SkillSoft Press.

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!

Thursday, December 11, 2008

My Fedora 10 Experience.


I got my new Compaque C60 laptop with AMD Athlon 64 bit dual core processor, 3GB RAM, NVidia Geforce 8200 Graphics, 160 GB Hard Drive and 16" widescreen monitor. And I installed Fedora 10 X86_64 in that.

At first it did not detect the graphics card and thus I had to install it in text mode. After which I downloaded the NVidia graphics driver and installed it and the graphics started working fine. I cannot live without music :D, so I first installed the livna repository, and installed xine-extras-nonfree with yum. Then I installed mplayer.

Since I am a java programmer, I installed jdk and netbeans. There was one thing very annoying though, Sun's jdk for 64 bit does not have a 64 bit browser plugin. I'm still waiting for its availability.

The webcam does get detected and configured automatically. I can use my webcam with "Cheese Webcam Booth" and with mplayer, But, still I cannot use it with kopete.

The graphical network manager does not work properly, it sets the default gateway same as the subnet musk. So I ran system-config-network from the console.

The overall experience with Fedora 10 is good untill now.