Category Archives: Software

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
Hello[/cc]

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: com.microsoft.sqlserver.jdbc.SQLServerDriver
at FetchData.fetch(FetchData.groovy:51)
at FetchData.run(FetchData.groovy:3)
[/cc]

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: com.microsoft.sqlserver.jdbc.SQLServerDriver
at FetchData.createCSV(FetchData.groovy:51)
at FetchData.run(FetchData.groovy:3)
[/cc]

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!
[/cc]

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 http://marxsoftware.blogspot.com/2009/05/groovysql-groovy-jdbc.html
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}.”
}

[/cc]

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 {
         println it.name
     }
 

[/cc]

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′)
import groovyx.net.ws.WSClient

proxy = new WSClient(“http://www.w3schools.com/webservices/tempconvert.asmx?WSDL”, this.class.classLoader)
proxy.initialize()

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

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: Smooth Calendar

One main reason I like Android over iPhone is widgets. With widgets I can, at a glance, look at the information I need. In iPhone I had to open an app, waiting for it to load and then finding the information I need. Calendar information is something I want to quickly get to. Smooth Calendar is the widget I like the most. And it is free. Try it out.

The following screenshot has the Smooth Calendar below the weather time widget.  The weather widget is Beautiful Widgets.

Android App Spotlight: DailyStrip

I like reading a few comic strips like Dilbert, Garfield. I used to subscribe to them via a RSS feed. But I sometimes miss reading them because there are so many posts in my Google Reader. DailyStrip is a comic strip reader that has access to more than 100 comic strips. I can select my favorites and view them everyday. If I miss a day I can step back and view them too. Check it out if you are into comic strips.

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.

Android App Spotlight: Tasker

It does not matter if there are only 100,000 apps in the Android Market or 300,000 in the Apple App Store. There are some great Android apps out there. Some apps do stuff only iPhone apps can dream off. ;-) The kidding aside, I am planning to write a series of blog posts spotlighting cool apps in the Android Market. The posts will not be complete reviews just highlights and I will try to link to a moew through review.

The first one is my favorite app: Tasker. Tasker can completely automate just about anything on your Android phone. The basic stuff I use it for are:

1. Setting my phone to only vibrate when at work, chruch, movie theaters etc. automatically based on time or location
2. Silencing my phone completely during sleep time, removing all visual notifications too
3. Automatically launch the music player or Google Listen when I connect my phone to my car stereo
4. Turn off WiFi at work 
5. Make Your Phone Quiet Down When Face-Down 

Here are some more use cases from the Tasker site:

  • passcode-lock sensitive applications (e.g. for child safety)
  • change phone settings by
    • application: long screen timeout in a book reader
    • time: screen brightness lower in the evening
    • location: ringer volume high at the office, turn off ke yguard at home
  • wake up with a random song from your music collection
  • Text-to-speech; read out loud: incoming SMS/ phone number, WiFi/Bluetooth status, when it’s time for an appointment, when the battery is low etc etc (Android OS 1.6+ only)
  • launch a music application when your music SD card is inserted, otherwise a file browser
  • start the day with a particular application showing
  • change all your home icons and wallpaper every day, or in particular locations
  • turn the phone upside down to return to the home screen, tilt 90 degrees to the left and back to toggle speakerphone during a call
  • create a Home widget to
    • toggle bluetooth/wifi on/off
    • launch wireless settings dialog
    • show a menu of tasks to choose
    • send an emergency SMS with your GPS location
  • remap camera etc buttons to other applications, or show a menu of applications and/or actions
  • decrypt/encrypt and/or zip/unzip application data on the fly when an application is launched/exits
  • pause music playback while in a particular application, restart on exit
  • change the Home icon for any application
  • take a time-lapse photo series (possibly ‘secretly’)
  • make a regular backup of a file on the SD card
  • track your phone location via SMS in case of theft
  • extend the use of the media button on your headset: take a picture from a distance or go to the previous media track with a long press
  • record call times and destinations to the SD card
  • show a popup when an SMS arrives from a particular phone number
  • setup a birthday SMS to be sent months before it happens so you don’t forget
  • record battery levels over time to a file on SD card
  • make automatic recordings of what you say during phone calls to SD card
  • during the night, turn on airplane mode to conserve battery/reduce radiation, but turn it off every 15 minutes to check for SMS/voicemail.
  • setup a vacation SMS message, with different messages for different callers
  • launch a music application when headphones are connected

 

Read more about its awesomeness at lifehacker

Get it: [AppBrain] [Market]

Readability – Super cool bookmarklet

I have to say I tried the Readability bookmarklet again after reading that Apple used its code in Safari’s Reader feature. Now it is my favorite way of reading articles.

Readability™ is a simple tool that makes reading on the Web more enjoyable by removing the clutter around what you’re reading. Follow the steps below to install Readability™ in your Web browser. [Readability]

I love the way the bookmarklet removes the unnecessary elements of the page and presents the article in a nice big font. I cant go back to reading articles with out this anymore. Give it a try.