Category Archives: Java

Running standalone Groovy scripts

As I mentioned in my post Learning Groovy, I am writing Groovy scripts to automate some simple tasks. Now I want to share the scripts with others. I did not want them to install Groovy just to run the scripts. I had the groovy-all-1.8.0.jar, so I typed the following in the command prompt:

[cc lang=’dos’ ]C:\temp>java -jar groovy-all-1.8.0.jar Hello.groovy

It worked. Simple. Now I tried to run my script that retrieved the XMLs from a database.

[cc lang=’dos’ ]
C:\work\FetchData>java -jar groovy-all-1.8.0.jar FetchData.groovy
Caught: java.lang.ClassNotFoundException:
at FetchData.fetch(FetchData.groovy:51)

Of course I need the driver jar in the classpath. So I added the sqljdbc.jar to the classpath.

[cc lang=’dos’ ]
C:\work\FetchData>java -cp sqljdbc.jar -jar groovy-all-1.8.0.jar FetchData.groovy
Caught: java.lang.ClassNotFoundException:
at FetchData.createCSV(FetchData.groovy:51)

Looks like the script is run with a different classloader and the java classpath is not passed to it. I did some lazy googling but did not find the answer I was looking for. So I looked at how eclipse is doing it. Eclipse used GroovyStarter and GroovyMain to run scripts. Not knowing which to use, I just used GroovyMain:

[cc lang=’dos’ ]
C:\work\FetchData>java -cp lib/groovy-all-1.8.0.jar;lib/sqljdbc.jar groovy.ui.GroovyMain FetchData.groovy
Fetched Data!

It worked! I just added the command in a batch file to share the script.

Infinitest: A continuous test runner for Java.

Infinitest is a continuous test runner for Java.

Infinitest is an Eclipse plugin that runs the JUnit tests instantly when you save a Java file. Check out the demo above. It smartly runs only the test affected by the file you just saved. This is cool. You can immediately know if you broke something as soon as you save your code. Of course, your test cases should be comprehensive. Improving Works developed this plugin and has released it as a open source project.

Learning Groovy

I have dabbled a bit with scripting languages before but for some reason never really used one long term. I think that might change with Groovy. A colleague of mine got me interested in it and I have been picking up all the cool things Groovy can do by reading Dustin’s “Inspired by Actual Events” blog. Come to think of it Groovy could have saved me a ton of time I spent in writing small tools to help me in development.

I like the how concise and simple writing groovy scripts are. Here is a snippet of code to print rows retrieved from a database.

[cc lang=’groovy’ ]

// Get instance of Groovy’s Sql class
// See
import groovy.sql.Sql

def sql = Sql.newInstance(“jdbc:oracle:thin:@localhost:1521:orcl”, “hr”, “hr”, “oracle.jdbc.pool.OracleDataSource”)

// iterate over query’s result set and “process” each row by printing two names
sql.eachRow(“SELECT employee_id, last_name, first_name FROM employees”)
println “Employee ${it.first_name} ${it.last_name} has ID of ${it.employee_id}.”


At work I needed to retrieve about 100 XMLs stored in a database, strip out some tags from it and write it to a file. It took about 15 lines of Groovy code to do it. Probably could have done it in less but I am just learning Groovy.

Here is another way to use Groovy that I wish I knew before. Embedding Groovy in Ant:

[cc lang='xml' ]

     project.references.found.each {


Though the above snippet is simple, it shows how the Groovy script is aware of the Ant references. Pretty powerful.

I will showcase just another snippet before I am off to learn more Groovy. Invoking a web service:

[cc lang=’groovy’ ]
@Grab(group=’org.codehaus.groovy.modules’, module=’groovyws’, version=’0.5.2′)

proxy = new WSClient(“”, this.class.classLoader)

result = proxy.CelsiusToFahrenheit(0)
println “You are probably freezing at ${result} degrees Farhenheit”

Droid Save/Droid Save IP Updated

I have updated a couple of my Android applications in the Market. The apps are simple. They save any links you share to either Instapaper or Read It Later services. The main new feature is offline capability. You can save links even when you have no internet connection and once the connection is back the apps will save the links. Here are the change logs and where you can get them:

Droid Save [Product Page | Market | AppBrain]

Version 1.6

1. Added offline capability. If there is no internet connection the URLs are saved locally and then sent to Read It Later when connection is back.
2. Added support for special characters in passwords like  ”<”, “|”.

Droid Save IP [Product Page | Market | AppBrain]

Version 1.2

1. Added offline capability. If there is no internet connection the URLs are saved locally and then sent to Read It Later when connection is back.
2. Added support for special characters in passwords like  ”<”, “|”.
3. Added support for accounts without password.

I developed these applications for me to use plus to learn Android development.

Android App Spotlight: AppBrain

Google has a site for Android Market which list all the apps available in the Market. But it is not a very user friendly site to navigate and find apps. I does not even have a search feature! Whats Google’s main claim to fame again? Anyway once you stumble on an interesting app there is no way to get the app. You will have to go to Market on your phone and search for the app from there. Clumsy! The other shot comings are: no rating, no comments, no sorting on popularity etc.

Here is where AppBrain steps in and shows Google how it should be. AppBrain has three key components:

  1. AppBrain – The site.
  2. AppBrain App Market – The App
  3. Fast Web Installer – Another App

The site makes up for all the things lacking in the official site. It has search, ratings, sorting, comments (from the market and its own comments). It also has the ability to add apps to your device(s). There are two ways to do this. First add it to your phone’s app list and then sync with the first AppBrain App Market. Second it to use the Fast Web Installer and send the app to your phone instantly. I use this a lot.

AppBrain also has a slew of social features. You can share the apps in your phone or just create a list of apps to share. I have created a list of all the apps I have talked about in this blog. It is available in here and in the side column in the blog. You can follow users and find the apps they are using.

It also provides a lot of useful statistics for developers about the OS versions and demographics of the users.

Try it out. Read more about it at Lifehacker.

Droid Save: My first Android app

I like my Android phone a lot. I almost always use it to read tweets, email and follow blogs on Google Reader. As good as the 4 inch Super AMOLED screen is, sometimes I like to read longer articles on my PC. I usually send an email to myself using the share feature most apps in Android have. It works but is kind of cumbersome. I was looking for apps to share links to sites like Read It Later or InstaPaper.  They did not have good support for Android. Read It Later had a few third party apps using their API but they did not do exactly what I wanted. So I created my own App.

It is very interesting to learn to program for Android. I this the OS is well designed and easy to develop for. At least for the simple use case I had.

Once installed here is what Droid Save does:

1. Shows up in the share actions when you share something from your twitter client, browser…

2. When you select “Droid Save”, the first time it prompts for the Read It Later credentials.

3. Once your credentials are validated and on subsequent uses it will either save the URL without further prompts or show a list of URLs if the shared text has multiple URLs. Selecting one saves it to Read It Later.

You can find Droid Save in AppBrain or in Android Market from your device. Or use the following QR Code.

Bugs/Comments are welcome.

Eclipse Plugin: StartExplorer


When I am working with Eclipse quite a few times I would copy a file’s path or want to open a location in Command Prompt to run some scripts. This is always cumbersome. I have to go to the properties and copy the path, go to command prompt, cd to the location…

StartExplorer is a nice plugin that does all of these and more. The screenshot should say a lot… Options like show resource in explorer, start cmd.exe here topped with keyboard shortcuts. Saves a lot of time for me everyday. You can create custom commands too.

JD: Amazing Java Decompiler


I just found this cool Java Decompiler, JD from Alon Aizenberg’s Tech Blog. It is just a single small exe (682kb) that can open jar files and decompile classes in an instant. The look and feel of the application is close to Eclipse. But it is blazingly fast. There is an Eclipse plugin too. Check it out.

Long time ago I posted about needing a tool like Reflector for Java. I needed a tool that I can use to quickly open a jar and browse the classes in it. JD comes pretty close. It would be perfect if it showed the outline of classes too.

How to find the version a Java class was compiled with?

I wanted to lookup the Java version a class was compiled with. I was getting an error:

java.lang.UnsupportedClassVersionError: (MyClass) bad major version at offset=6

Thanks to StackOverflow I found out the following command I did not know before:

javap -verbose MyClass

The major version tells the Java version used. Here are some example values:

* Java 1.2 uses major version 46
* Java 1.3 uses major version 47
* Java 1.4 uses major version 48
* Java 5 uses major version 49
* Java 6 uses major version 50

I am blogging this for future reference.