Saturday, 7 December 2013

Groovy: G'day World

G'day:
I'm investigating some ColdFusion stupidity (more on that after this article), and as part of my comparison of the behaviour in other languages, I figured I should also test on another JVM-based language. And given all the adulation of Groovy at the moment, and an exceptional presentation "Grails/Groovy Primer for ColdFusion Developers" from Scott Stroz, I decided to D/L and install Groovy.

Warning: this will be a short article, and will only take me as far as getting "G'day World" onto the screen. It's really aimed at CFMLers like myself who never find time to do anything else, and just a demonstration as to how quick this all was.

Previously I've written up similar exploits on PHP: "PHP: from zero to... Hello World" and Ruby: "Ruby: stream-of-consciousness".

OK, so here's what I did to get Groovy up and running on my machine:
  • Googled "Groovy" to find the Groovy website.
  • On the download page, there's a Windows installer. Download took a coupla min: it's 73MB (reminder: PHP is 16MB, Ruby was 17MB, Railo is 64MB; CF10 is 350MB).
  • I ran the installer and followed the default options except changed the install dir, as part of this I opted to add the bin dir onto the path so I could run Groovy from anywhere.
  • At the end of the install it said it could not find the JAVA_HOME environment variable, so I needed to set one of those up. This surprised me (that it couldn't find it), but I confirmed that I didn't have one set for some reason. I did rebuild this machine a few months back, and perhaps I've done no Java (something I do only occasionally) since then. Anyway, I set that up and pointed it to a 64-bit JDK I have installed.
That's it. It took about 10min altogether, including googling, download and install time. And checking into and dealing with the JAVA_HOME thing.


Of course I still didn't know if it actually worked, so I googled up some "Hello world" code to try, and went to try to run it.

The Groovy bin dir lists this stuff:

C:\apps\Groovy\Groovy-2.2.1\bin>dir
 Volume in drive C has no label.
 Volume Serial Number is A473-4DFF

 Directory of C:\apps\Groovy\Groovy-2.2.1\bin

07/12/2013  12:14    <DIR>          .
07/12/2013  12:14    <DIR>          ..
06/01/2013  17:34             3,629 gant
18/03/2012  16:33             2,620 gant.bat
18/03/2011  15:13            52,224 gant.exe
18/03/2011  15:13            52,224 gantw.exe
26/11/2013  12:23               986 grape
26/11/2013  12:23               471 grape.bat
26/11/2013  12:23               972 groovy
26/11/2013  12:23               456 groovy.bat
18/03/2011  15:13            52,224 groovy.exe
26/11/2013  12:23               839 groovyc
26/11/2013  12:23               480 groovyc.bat
18/03/2011  15:13            52,224 groovyc.exe
26/07/2013  13:49           248,986 groovyclient.exe
26/07/2013  08:43             8,125 groovyclient.sh
26/11/2013  12:23               818 groovyConsole
26/11/2013  12:23               453 groovyConsole.bat
18/03/2011  15:13            52,224 groovyConsole.exe
26/11/2013  12:23               837 groovydoc
26/11/2013  12:23               476 groovydoc.bat
25/07/2013  10:50             7,519 groovyserver
22/07/2013  15:44             9,146 groovyserver.bat
26/11/2013  12:23               833 groovysh
26/11/2013  12:23               498 groovysh.bat
18/03/2011  15:13            52,224 groovysh.exe
18/03/2011  15:13            52,224 groovyw.exe
08/04/2009  16:43           117,760 jacob-1.14.3-x64.dll
08/04/2009  16:43           102,400 jacob-1.14.3-x86.dll
26/11/2013  12:23               899 java2groovy
26/11/2013  12:23               536 java2groovy.bat
18/03/2011  15:13            52,224 java2groovy.exe
09/10/2007  15:06           626,688 msvcr80.dll
26/11/2013  12:23             8,441 startGroovy
26/11/2013  12:23             7,367 startGroovy.bat
22/07/2013  15:44             3,797 _common.sh
              36 File(s)      1,573,973 bytes
               2 Dir(s)  240,017,940,480 bytes free

C:\apps\Groovy\Groovy-2.2.1\bin>

Looking at that, I suspected there were two things to try: initially groovysh, for running stuff directly, and also just groovy, for running files. Or that was my guess. I had not done any RTFM or anything like that.

I ran groovysh, and got this:


C:\apps\Groovy\Groovy-2.2.1\bin>groovysh
error: dynamic library C:\apps\oracle\Java\jdk\64\1.7.0_17\jre\bin\server\jvm.dl
l exists but could not be loaded!
This may be caused e.g. by trying to use a 32-bit executable to load a 64-bit jv
m (or vice versa)
error (win code 193): (null)
error: could not find client or server jvm under C:\apps\oracle\Java\jdk\64\1.7.
0_17\jre
       please check that it is a valid jdk / jre containing the desired type of
jvm

C:\apps\Groovy\Groovy-2.2.1\bin>


It then occurred to me I didn't notice that there was an alternative installer for 32- or 64-bit, so assumed the installer worked it out for itself. Seemingly not. I had a 32-bit install of Groovy. I'm not that fussed about that, but I didn't want my global JAVA_HOME setting pointing to a 32-bit JDK, so I knocked together a quick batch file to wrap the call to groovysh, and set a 32-bit environment just for it:

@echo off
set java_home=C:\apps\oracle\Java\jdk\32\1.7.0_07\jre
groovysh
pause

Now running that, I'm away laughing:


C:\apps\Groovy\Groovy-2.2.1\bin>groovysh32.bat
Groovy Shell (2.2.1, JVM: 1.7.0_07)
Type 'help' or '\h' for help.
-------------------------------------------------------------------------------
groovy:000>


And I type my G'day World code in...


groovy:000> msg = "G'day World"
===> G'day World
groovy:000> println msg
G'day World
===> null
groovy:000>

And that's my first executed Grooy code ever. Cool. That was really easy (I did have to google the syntax, but that's fair enough).

Next I wanted to try the CLI instead.

So I saved my file.. well... err... do you know what? I'm so bloody ignorant of Groovy I didn't even know what the standard file extension was! Now I know it doesn't really matter, but I want to know these things, and found out it's just .groovy. I possibly coulda guessed that.

The file version of the code is the same as above:

// gdayWorld.groovy
msg = "G'day World"
println msg

I also pre-empted I'd need to wrap up groovy.exe in a batch file too, which I did:

@echo off
set java_home=C:\apps\oracle\Java\jdk\32\1.7.0_07\jre
groovy %1

Running that, I got another thumbs-up from Groovy:


C:\webroots\shared\git\blogExamples\otherLanguages\groovy>groovy32 gdayWorld.groovy
G'day World

C:\webroots\shared\git\blogExamples\otherLanguages\groovy>

Coolio. I was all good to go with comparing how CF does something to how Groovy does the same thing now... stay tuned (warning: it is not very interesting)...

--
Adam