Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Sign Up
Dashboard
Groovy
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<h1>Getting Started</h1><h2>Setting up your Java environment</h2><p>Groovy requires Java, so you need to have a version available (while groovy 1.6 supported JDK 1.4 or greater, for groovy 1.7 onwards, minimum JDK 1.5 is needed). Here are the steps if you don't already have Java installed:</p><ul><li>Get the suitable Java distribution from the <a href="http://java.sun.com">http://java.sun.com</a> website.</li><li>Run the installer.</li><li>Set the JAVA_HOME environment variables. On Windows, follow these steps:<ul><li>Open the System control panel</li><li>Click the Advanced tab</li><li>Click the Environment Variables button</li><li>Add a new System variable with the name JAVA_HOME and the value of the directory Java was installed in (mine is C:\Program Files\Java\jdk1.5.0_04)</li><li>Optionally add %JAVA_HOME%\bin to your system path<br /> (Note: as an alternative to setting a system environment variable, you can create yourself a '.bat' or '.cmd' file which sets the variable. You then need to run that batch file in any console window in which you wish to run Java and double clicking on '.bat' or '.cmd' files containing Java invocation instructions won't work. If you are unsure about what this means, follow the earlier instructions.)</li></ul></li></ul><h2>Setting up your Groovy environment</h2><p>Download the Groovy installer or binaries from the <a href="http://groovy.codehaus.org/Download">downloads </a>page and follow the installation instructions. (There is currently an issue where you cannot have spaces in the path where Groovy is installed under windows. So, instead of accepting the default installation path of "c:\Program Files\Groovy" you will want to change the path to something like "c:\Groovy")</p><p>OR</p><ul><li>Get a copy of the Groovy distribution from the <a href="http://dist.codehaus.org/groovy/distributions/">website</a>, and copy it to some place on your hard drive.</li><li>Unzip the groovy archive to some logical place on your hard drive, I have mine in C:\dev\groovy-2.0.5</li><li>Set the GROOVY_HOME environment variables. On Windows, follow these steps:<br /><ul><li>Optionally add a new System variable with the name GROOVY_HOME and the value of the directory groovy was installed in (mine is C:\dev\groovy-2.0.5)</li><li>Start a command prompt, and type "set" and hit return to see that your environment variables were set correctly.</li></ul></li><li>Optionally add %GROOVY_HOME%\bin to your system path</li><li>Try opening groovyConsole.bat by double clicking on the icon in the bin directory of the Groovy distribution. If it doesn't work, open a command prompt, and change to the bin directory and run it from there to see what the error message is. If it is complaining about not finding parts of Groovy, then setting GROOVY_HOME may help,</li></ul><h2>Setting up optional jar files</h2><p>You may wish to obtain optional jar files, either corresponding to Groovy modules (see module documentation for details) or corresponding to other Java classes you wish to make use of from Groovy. Some possibilities are listed below:</p><table class="confluenceTable"><tbody><tr><th class="confluenceTh"><p>Name</p></th><th class="confluenceTh"><p>From</p></th><th class="confluenceTh"><p>Description</p></th></tr><tr><td class="confluenceTd"><p>jtds-<em>version</em>.jar</p></td><td class="confluenceTd"><p><a href="http://jtds.sourceforge.net">http://jtds.sourceforge.net</a></p></td><td class="confluenceTd"><p>Database driver for SQL Server and/or Sybase</p></td></tr><tr><td class="confluenceTd"><p>hsqldb-<em>version</em>.jar</p></td><td class="confluenceTd"><p><a href="http://www.hsqldb.org/">http://www.hsqldb.org/</a></p></td><td class="confluenceTd"><p>Database driver for HSQLDB, a 100% Java database</p></td></tr></tbody></table><p>The recommended way for making Groovy be aware of your additional jar files is to place them in a predefined location. Your Groovy install should include a file called <code>groovy-starter.conf</code>. Within that file, make sure a line such as</p><table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="none" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6bm9uZX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>load ${user.home}/.groovy/lib/* </pre></td></tr></table><p><br class="atl-forced-newline" /> is not commented out. The <code>user.home</code> system property is set by your operating system. (Mine is <code>C:\Document and Settings\paul</code>. Now simply place your jar files into the <code>.groovy/lib</code> directory.<br /> (Note: as an alternative, you can set up a CLASSPATH variable and make sure it mentions all of your additional jar files, otherwise Groovy works fine with an empty or no CLASSPATH variable.)</p><h2>Hello, World</h2><p>In the top part of the window of the groovyConsole, type the following</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>println "Hello, World!" </pre></td></tr></table><p><br class="atl-forced-newline" /> And then type <CTRL-R>.<br /> Notice that the text gets printed out in the OS console window (the black one behind the groovyConsole window) and the bottom part of the groovyConsole says:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>groovy> println "Hello, World!" null </pre></td></tr></table><p><br class="atl-forced-newline" /> The line starting with "groovy>" is just the text of what the console processed. The "null" is what the expression "evaluated to". Turns out the expression to print out a message doesn't have any "value" so the groovyConsole printed "null".<br /> Next try something with an actual value. Replace the text in the console with:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>123+45*67 </pre></td></tr></table><p><br class="atl-forced-newline" /> or your favorite arithmetic expression, and then type <CTRL-R> (I'm going to stop telling you to hit <CTRL-R>, I think you get the idea). Now the "value" printed at the bottom of the groovyConsole has more meaning.</p><h2>Variables</h2><p>You can assign values to variables for later use. Try the following:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>x = 1 println x x = new java.util.Date() println x x = -3.1499392 println x x = false println x x = "Hi" println x </pre></td></tr></table><p> </p><h2>Lists and Maps</h2><p>The Groovy language has built-in support for two important data types, lists and maps (Lists can be operated as arrays in Java language). Lists are used to store ordered collections of data. For example an integer list of your favorite integers might look like this:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>myList = [1776, -1, 33, 99, 0, 928734928763] </pre></td></tr></table><p><br class="atl-forced-newline" /> You can access a given item in the list with square bracket notation (indexes start at 0):</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>println myList[0] </pre></td></tr></table><p><br class="atl-forced-newline" /> Should result in this output:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>1776 </pre></td></tr></table><p><br class="atl-forced-newline" /> You can get the length of the list with the "size" method:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>println myList.size() </pre></td></tr></table><p><br class="atl-forced-newline" /> Should print out:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>6 </pre></td></tr></table><p><br class="atl-forced-newline" /> But generally you shouldn't need the length, because unlike Java, the preferred method to loop over all the elements in an list is to use the "each" method, which is described below in the "Code as Data" section.<br /> Another native data structure is called a map. A map is used to store "associative arrays" or "dictionaries". That is unordered collections of heterogeneous, named data. For example, let's say we wanted to store names with IQ scores we might have:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>scores = [ "Brett":100, "Pete":"Did not finish", "Andrew":86.87934 ] </pre></td></tr></table><p><br class="atl-forced-newline" /> Note that each of the values stored in the map is of a different type. Brett's is an integer, Pete's is a string, and Andrew's is a floating point number. We can access the values in a map in two main ways:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>println scores["Pete"] println scores.Pete </pre></td></tr></table><p><br class="atl-forced-newline" /> Should produce the output:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>Did not finish Did not finish </pre></td></tr></table><p><br class="atl-forced-newline" /> To add data to a map, the syntax is similar to adding values to an list. For example, if Pete re-took the IQ test and got a 3, we might:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>scores["Pete"] = 3 </pre></td></tr></table><p><br class="atl-forced-newline" /> Then later when we get the value back out, it will be 3.</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>println scores["Pete"] </pre></td></tr></table><p><br class="atl-forced-newline" /> should print out 3.<br /> Also as an aside, you can create an empty map or an empty list with the following:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>emptyMap = [:] emptyList = [] </pre></td></tr></table><p><br class="atl-forced-newline" /> To make sure the lists are empty, you can run the following lines:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>println emptyMap.size() println emptyList.size() </pre></td></tr></table><p><br class="atl-forced-newline" /> Should print a size of 0 for the List and the Map.</p><h2>Conditional Execution</h2><p>One of the most important features of any programming language is the ability to execute different code under different conditions. The simplest way to do this is to use the '''if''' construct. For example:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>amPM = Calendar.getInstance().get(Calendar.AM_PM) if (amPM == Calendar.AM) { println("Good morning") } else { println("Good evening") } </pre></td></tr></table><p><br class="atl-forced-newline" /> Don't worry too much about the first line, it's just some code to determine whether it is currently before noon or after. The rest of the code executes as follows: first it evaluates the expression in the parentheses, then depending on whether the result is '''true''' or '''false''' it executes the first or the second code block. See the section below on boolean expressions.<br /> Note that the "else" block is not required, but the "then" block is:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>amPM = Calendar.getInstance().get(Calendar.AM_PM) if (amPM == Calendar.AM) { println("Have another cup of coffee.") } </pre></td></tr></table><p> </p><h2>Boolean Expressions</h2><p>There is a special data type in most programming languages that is used to represent truth values, '''true''' and '''false'''. The simplest boolean expression are simply those words. Boolean values can be stored in variables, just like any other data type:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>myBooleanVariable = true </pre></td></tr></table><p><br class="atl-forced-newline" /> A more complex boolean expression uses one of the boolean operators:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>* == * != * > * >= * < * <= </pre></td></tr></table><p><br class="atl-forced-newline" /> Most of those are probably pretty intuitive. The equality operator is '''==''' to distinguish from the assignment operator '''='''. The opposite of equality is the '''!=''' operator, that is "not equal"<br /> So some examples:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>titanicBoxOffice = 1234600000 titanicDirector = "James Cameron" trueLiesBoxOffice = 219000000 trueLiesDirector = "James Cameron" returnOfTheKingBoxOffice = 752200000 returnOfTheKingDirector = "Peter Jackson" theTwoTowersBoxOffice = 581200000 theTwoTowersDirector = "PeterJackson" titanicBoxOffice > returnOfTheKingBoxOffice // evaluates to true titanicBoxOffice >= returnOfTheKingBoxOffice // evaluates to true titanicBoxOffice >= titanicBoxOffice // evaluates to true titanicBoxOffice > titanicBoxOffice // evaluates to false titanicBoxOffice + trueLiesBoxOffice < returnOfTheKingBoxOffice + theTwoTowersBoxOffice // evaluates to false titanicDirector > returnOfTheKingDirector // evaluates to false, because "J" is before "P" titanicDirector < returnOfTheKingDirector // evaluates to true titanicDirector >= "James Cameron" // evaluates to true titanicDirector == "James Cameron" // evaluates to true </pre></td></tr></table><p><br class="atl-forced-newline" /> Boolean expressions are especially useful when used in conjunction with the '''if''' construct. For example:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>if (titanicBoxOffice + trueLiesBoxOffice > returnOfTheKingBoxOffice + theTwoTowersBoxOffice) { println(titanicDirector + " is a better director than " + returnOfTheKingDirector) } </pre></td></tr></table><p><br class="atl-forced-newline" /> An especially useful test is to test whether a variable or expression is null (has no value). For example let's say we want to see whether a given key is in a map:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>suvMap = ["Acura MDX":"\$36,700", "Ford Explorer":"\$26,845"] if (suvMap["Hummer H3"] != null) { println("A Hummer H3 will set you back "+suvMap["Hummer H3"]); } </pre></td></tr></table><p><br class="atl-forced-newline" /> Generally null is used to indicate the lack of a value in some location.</p><h1>Debugging and Troubleshooting Tips</h1><ul><li>Print out the class of a variable that you're interested in with myVar.getClass(). Then look up the documentation for that class.</li><li>If you're having trouble with a complex expression, pare it down to a simpler expression and evaluate that. Then build up to your more complex expression.</li><li>Try restarting the groovyConsole (this will clear out all the variables so you can start over.</li><li>Look for the topic you're interested in in the Groovy <a class="confluence-link" href="/display/GROOVY/User+Guide" data-linked-resource-id="2720" data-linked-resource-type="page" data-linked-resource-default-alias="User Guide" data-base-url="http://docs.codehaus.org">User Guide</a></li></ul><h1>If you are a Java developer</h1><ul><li>you might want to check on the <a class="confluence-link" href="/display/GROOVY/Differences+from+Java" data-linked-resource-id="2756" data-linked-resource-type="page" data-linked-resource-default-alias="Differences from Java" data-base-url="http://docs.codehaus.org">Differences from Java</a></li><li>also there a few <a class="confluence-link" href="/display/GROOVY/Things+to+remember" data-linked-resource-id="6553" data-linked-resource-type="page" data-linked-resource-default-alias="Things to remember" data-base-url="http://docs.codehaus.org">Things to remember</a></li></ul>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced