Archive for the ‘unix’ Category



3
Aug

Automatically resolving jars for the Java classpath

When you use the Java classpath (pre java 1.6) you have to manually list each jar on your classpath such as:

java -cp lib/database.jar:lib/commons.jar:/lib/log.jar com.joejag.Main

On Java 1.6 a little known feature is that you can now use wildcards, so the above command becomes:

java -cp lib/*.jar com.joejag.Main

I still have to use an earlier version of Java for some applications I handle. To save having to list all the jars manually I use the following bash script which allows you to automatically list all the jars in a directory:

java -cp `find lib -name *.jar -exec printf :{} ';'` com.joejag.Main
3
Jul

Using Ruby as an AWK replacement

Someone at work asked if you could use Ruby like AWK.  I did a little digging and found that you can.

cat file | ruby -n -a -e 'puts "#{$F[0] $F[1]}"'

‘-n’ makes the Ruby iterate over all lines successively assigning them to $_
‘-a’ makes Ruby split $_ into its parts assigning the result to $F which is an array of strings
‘-e’ means that what follows is code to be executed.
‘-F’ specifies the column separator

I performed a speed comparison on some different size files and operations. For files under 500kb lines Ruby has comparable performance to AWK. For anything larger then Ruby (1.8.6) is at best twice as slow. Though I wouldn’t expect a general purpose language to outperform a specialist tool.