Friday, April 2, 2010

Groovy Getting started, How use groovy in Java, Groovy easy example, Groovy fundamental

What is Groovy?

The Groovy web site (http://groovy.codehaus.org/) gives one of the best definitionsof Groovy: “Groovy is an agile dynamic language for the Java Platform with manyfeatures that are inspired by languages like Python, Ruby and Smalltalk, makingthem available to Java developers using a Java-like syntax.

Everything you do in Groovy could be done in Java, it would be madness to writethe Java code required to work Groovy’s magic. Groovy performs a lot of workbehind the scenes to achieve its agility and dynamic nature.

The Groovy solution is short, precise, and more compact than normal Java. Groovy does not need to import the java.util package or specify the Date type;moreover, Groovy doesn’t require semicolons when it can understand the code without them.


import java.util.*; // Java
Date today = new Date(); // Java

today = new Date() // Groovy

How to install Groovy.

1. Download groovy-src-1.7.0.zip from
http://dist.codehaus.org/groovy/distributions/
2. Unzip this file and put any where in your pc. I have put at D:\Software\Groovy\groovy-1.7.0
3. Add a new System variable with the name GROOVY_HOME and the value of the directory groovy was installed in (In my case it is D:\Software\Groovy\groovy-1.7.0
4. add %GROOVY_HOME%\bin to your system path
5. Now open any dos window and type groovy. If every this is correct then you will get some opetion otherwise you will get error message like 'groovy' is not recognized as an internal or external command,operable program or batch file.

REF:
http://groovy.codehaus.org/Tutorial+1+-+Getting+started

Make one directory to put your all example code. In my case D:\Nik\Groovy_Example

FIRST GROOY CODE

HelloWorld.groovy

class HelloWorld
{
static main( args )
{ println "Hello World!" }
}

How to compile groovy file
Open dos window and navigate to D:\Nik\Groovy_Example
D:\Nik\Groovy_Example>groovyc HelloWorld.groovy
D:\Nik\Groovy_Example>

Now you will check one new class file (HelloWorld.class) has been generate in this folder (D:\Nik\Groovy_Example).

How to run the groovy class
D:\Nik\Groovy_Example>groovy HelloWorldHello World!
D:\Nil\Groovy_Example>
Even without compile the code you can direct use groovy command to run the code Like

D:\Nik\Groovy_Example>groovy HelloWorldor
D:\Nik\Groovy_Example>groovy HelloWorld.groovy

you can also use extension like gv or groovy

Here below, I am giving one java code and after that will convert same java code to Groovy

Second.java

public class Second {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() { return name; }
public String greet() { return "Hello " + name; }

public static void main(String[] args) {
Second second = new Second();
second.setName("My name is groovy");
System.out.println(second.greet());
}
}

Second.gy

class HelloWorld {
def name
def greet() { "Hello ${name}" }
}
def helloWorld = new HelloWorld()
helloWorld.name = "My name is groovy"
println helloWorld.greet()


NOTE: Groovy is an Object-Oriented language (a real one where numbers are also objects), and it supports the same programming model as Java. However, it's also a scripting language as it allows you to write some free form programs that don't require you to define a class structure. So in the last step of this tutorial, we're going to get rid of the main method altogether.

Both will give the same output.

D:\Nik\Groovy_Example>javac Second.java
D:\Nik\Groovy_Example>java SecondHello My name is groovy

D:\Nik\Groovy_Example>groovy Second.gyHello My name is groovy
D:\Nik\Groovy_Example>

Ref:
http://groovy.dzone.com/news/java-groovy-few-easy-steps

How to run groovy script using java program

Secong.gy

class HelloWorld {
def name
def greet() { "Hello ${name}" }
}

def helloWorld = new HelloWorld()
helloWorld.name = "My name is groovy TEST "
println helloWorld.greet()

GroovyTest.java

import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;
import java.io.File;

public class GroovyTest {

public static void main(String[] args) throws Exception{
System.out.println("THIS IS FIRST GROOVY PROGRAM");
new GroovyTest().GroovyRun();
}

public void GroovyRun()throws Exception{
ClassLoader parent = getClass().getClassLoader();
GroovyClassLoader loader = new GroovyClassLoader(parent);
Class groovyClass = loader.parseClass(new File("D:\\Nik\\Groovy_Example\\Second.gy"));
// let's call some method on an instance
GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
Object[] args = {}; groovyObject.invokeMethod("main", args);
}
}

D:\Nik\Groovy_Example>javac GroovyTest.java
D:\Nik\Groovy_Example>java GroovyTest
THIS IS FIRST GROOVY PROGRAMHello My name is groovy TEST

D:\Nik\Groovy_Example>







2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hi Ayushman..
    This is excellent post .
    However when I compile GroovyTest.java..i get
    package groovy.lang does not exist
    [javac] import groovy.lang.*; error.

    Please insist a QuickFix

    ReplyDelete