Use groovy to think about Chapter 3 Groovy Development Environment

Source: Internet
Author: User
Tags groovy script
Use groovy to think about Chapter 3 Groovy Development Environment

Author: chszs, reprinted with note. Blog homepage: http://blog.csdn.net/chszs

This chapter will continue with the groovy language. First, we will learn about groovy scripts, including compiling and running groovy scripts, groovy shell, and groovy console from the command line.

You will learn to use the groovy language to build domain objects, controllers, and services. In addition, there is not only one method to use groovy. In addition, groovy can be used as the scripting language for creating classes.

Example 1: simple groovy script hello. Groovy

Println "Hello $ {ARGs [0]}, May groovy be with you ."

Run the preceding commandCode:

Ps d: \ TMP \ groovy> groovy hello "Li sir"

Hello Li sir, may groovy be with you.

During script execution, groovy generates a class file with the same name as the source file, which contains the main method.

If the above Code is implemented by using Java, it will become longer.

Public class hellojava {

Public static void main (string [] ARGs ){

System. Out. println ("hello" + ARGs [0] + ", may Java Be With You .");

}

}

1. Use script Functions

Like most scripting languages, groovy scripts can be organized into reusable code blocks. These blocks are called functions. The following example shows how to create and use a function.

Def printfullname (firstname, lastname ){

Println "$ {firstname }$ {lastname }"

}

Printfullname ('Luke ', 'skywalker ')

Printfullname ('lil', 'Sir ')

Ps d: \ TMP \ groovy> groovy. \ printfullname. Groovy

Luke Skywalker

Li sir

2. Compile groovy

The above example omitted the groovy compilation process. Like java, groovy scripts can also be compiled into bytecode files. As follows:

Ps d: \ TMP \ groovy> groovyc hello. Groovy

We can see that a bytecode file named hello. Class is generated under the directory. However, we cannot directly use Java to execute it. To execute it using Java, you need to do this:

D: \ TMP \ groovy> JAVA-CP % groovy_home %/embeddable/groovy-all-2.1.2.jar;. Hello "Li sir"

Hello lisir, May groovy be with you.

You can use Java commands to run groovyProgramIt proves that groovy is Java. Add groovy-all-<version>. jar to the class path.

The Groovy compiler is a federated compiler that can compile both groovy code and Java code. Groovy's Integrated compiler was introduced in version 1.5, which was contributed by jetbrains (the company that invented the intellij idea development environment ). The federated compiler allows you to compile groovy and java files in a single-line compilation statement.

Example:

Name. Groovy code:

Public class sayhello {

Public static void main (string [] ARGs ){

Name = new name ();

Name. setfirstname (ARGs [0]);

System. Out. println (name. tostring ());

}

}

Sayhello. Java code:

Public class sayhello {

Public static void main (string [] ARGs ){

Name = new name ();

Name. setfirstname (ARGs [0]);

System. Out. println (name. tostring ());

}

}

Joint Compilation:

D: \ TMP> policyc *. Groovy *. Java

Run the Code:

D: \ TMP> JAVA-CP % groovy_home %/embeddable/groovy-all-2.1.2.jar;. sayhello

"Li sir"

Hello Li sir, Java calling groovy

3. Run groovy

You can run groovy scripts and classes through the command line, groovy shell, and groovy console.

3.1 command line

There are several options for running the groovy script in the command line:

(1) Use the groovy command directly in the command line.

In this way, groovy will generate a class containing the main () method of the script command, compile the script, and execute it. If you do not want to re-compile the file at runtime, you can use the third option.

(2) Compile the groovy script as a bytecode file and run it in Java.

As in the previous example.

(3) In Windows, the. Groovy extension can be associated with the groovy program. In a Unix environment, the following script can be used for the same purpose:

#! /Usr/bin/groovy

Println "Hello $ {ARGs [0]}, May groovy be with you ."

3.2 groovy Shell

Groovy shell is an interactive command line program that allows developers to create, run, save, and load groovy scripts and classes. To start groovy shell, you only need to run groovysh.

D: \ TMP \ groovy> policysh

Groovy shell (2.1.2, JVM: 1.7.0 _ 09)

Type 'help' or '\ H' for help.

------------------------------------------------------------

GROOVY: 000>


Enter help in groovy shell to learn and use some shell commands,

3.3 groovy Console

The Groovy console is a groovy shell with a graphical interface. The Groovy console is developed using swingbuilder to facilitate development.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.