How to Write a Basic Groovy Script

  • google plus

In Brief...

Getting started with Groovy can be easy and fun! To create and run a basic script written in Groovy, follow these three steps.


  1. Open your text editor and create the Groovy script:
    // Basic Groovy Script
    Console console=System.console();
    def name=console.readLine("What is your name? ")
    println "Welcome to Groovy, $name!"
    The program creates a Console object in order to read data typed in by the user. Then the program sends a greeting that contains the name provided by the user.
  2. Save your file as BasicScript.groovy.
  3. Open a command prompt and navigate to the directory containing your new Groovy program. Then type in the command to interpret and run your script:
    Run basic Groovy program
    When the program prompts you, type your name (I typed "Stephen") and hit Enter. The program will echo back your name with the greeting.

Author: Stephen Withrow

Stephen has over 30 years' experience in training, development, and consulting in a variety of technology areas including Java, C, C++, XML, JavaScript, AJAX, Tomcat, JBoss, Oracle, and DB2. His background includes design and implementation of business solutions on client/server, Web, and enterprise platforms. Stephen is a published writer in both technical and non-technical endeavors. Stephen received an undergraduate degree in Computer Science and Physics from Florida State University.