Posted by on Nov 1, 2011 in Programming | 0 comments

I recall building a small graphical user interface using a beta version of the ‘Swing AWT Extensions’ in the late 90’s and thinking to myself that it was going to change everything. Fast forward 12+ years, things have changed but much has stayed the same.

A good portion of the work in Calgary is still Java based. It is primarily an oil and gas city (read: IT is viewed as a liability not an asset) but from what I know Java still dominates non-Microsoft shops in major centers throughout North America. This baffles me as it equates to taking a large portion of your IT budget and burning it. Sure you can continue to run legacy Java applications much like banks continue to run COBOL on OS/390 systems. I don’t have a problem with it as long as it does what it is supposed to. The issue I have is when these systems are extended and modified to do things they weren’t meant to do or where there now exists many better alternatives, especially if you are doing web development. If you wanted to build an addition on your mud hut I would hope you would use modern building technics like brick and mortar instead of more mud. Ruby on Rails, Django and Grails beat traditional Java web frameworks by a large margin in almost every aspect you can measure. I realize throwing away 10+ years of Java code might feel like the wrong thing to do but when you realize JRuby, JPython and Groovy run on the Java Virtual Machine allowing access to your old Java libraries you are able to migrate without having to replace the working parts of your system.

I can’t really speak for Ruby or Python and their Java implementations JRuby and JPython at this time, as my experience with those languages is mostly academic. I have been using Groovy and Grails now for several years.

Hello World

This code is yanked directory from Oracles Hello World Java lesson. It is about as trivial as you can get.

class HelloWorldApp { public static void main(String[] args) { System.out.println("Hello World!"); // Display the string. } }
rootsh stan$ javac HelloWorldApp.java 
rootsh stan$ java HelloWorldApp
Hello World!
rootsh stan$

Interestingly enough the above is also a valid Groovy program. Without any changes you can just rename the extension .groovy and run it as follows.

rootsh stan$ groovy HelloWorldApp.groovy 
Hello World!
rootsh stan$

Of course that doesn’t really accomplish anything besides removing the javac compilation step. Groovy is also a scripting language you could also create a new file called helloworld.groovy with the following contents

println("Hello World!"); // Display the string.

and run it with:

rootsh stan$ groovy helloworld.groovy 
Hello World!
rootsh stan$

Or if you want to keep the class.

class HelloWorldApp { public static void main(args) { println("Hello World!") // Display the string. } }

Notice the lack of semi-colons, System.out and the String[] type in the main method definition. Semi-colons and type definitions are optional in Groovy. You can specify types if you want and they will be enforced at runtime. System.out is made available to every Groovy object and script along with a collection of imports like java.lang.*. http://groovy.org has lots of good bits on their site that I would recommend looking at. Current Java developers should have no difficulty picking Groovy up in as little as a day. It might take a bit longer to start thinking Groovy though. At first you write ‘Java code in Groovy’ before you start to think in terms of closures, dynamic typing, additional methods added to existing Java objects and things like def map = [:].

If you have a working Java application I would look at Groovyc Joint Compilation and embedding Groovy in your Java code.

twittergoogle_pluslinkedin