When Runtime.exec() won't - JavaWorld

A well written article on JavaWorld that explains how to effectively use the Runtime.exec() method. It really saved me a lot of time.

When Runtime.exec() won’t - JavaWorld

Javascript getElementById() shortcut

Do you love the wonderful javascript dollar shortcut $('myId') but cannot use jQuery or Prototype? Don’t worry, you can define your custom shortcut for the too long document.getElementById() function:

function $(id) {
	return document.getElementById(id);
}

Now you can sobstitute this:

var divContent = document.getElementById('myId').innerHTML;

with this:

var divContent = $('myId').innerHTML;

Obviously you don’t get all the bells and whistles that you would get using jQuery, but it’s a start.

Get location coordinates using Google Maps

This article uses **v2** version of Google Maps API. You sould use [v3][5] by now

This is a simple tutorial on finding location’s coordinates using Google Maps APIs.

First of all you need to signup for a Google Maps API key, otherwise your script will not work. When you are done, start building a simple form with three fields: one for the location and the others for coordinates display.

<form>
  Address:
  <input name="address" type="text" />
  <input onclick="getCoordinates()" type="button" value="Get coordinates" />
 
  Latitude:
  <input name="lat" type="text" />
  Longitude:
  <input name="lng" type="text" />
</form>

Now, we need to define the getCoordinates() function: this function must read the value of the address field, check if it’s valid and get its coordinates. To get coordinates we’ll use the GClientGeocoder class and its method getLatLng(address:String, callback:function):

Sends a request to Google servers to geocode the specified address. If the address was successfully located, the user-specified callback function is invoked with a GLatLng point. Otherwise, the callback function is given a null point. In case of ambiguous addresses, only the point for the best match is passed to the callback function.

var geocoder = new GClientGeocoder();

function getCoordinates(address){

  var address = document.myForm.address.value;

  if (address != ''){

    geocoder.getLatLng(address,function(point){

      if (point != null){
        document.myForm.lat.value = point.lat();
        document.myForm.lng.value = point.lng();
      }

      else{
        alert("Location not found");
      }
    })
  } else {
    alert("Please insert a location");
  }
}

Compact font styles

This is a simple tip that can help you reduce your style sheet size. Take a look at this CSS portion:

.myClass{
	font-family: Arial, Verdana, Helvetica, sans-serif;
	font-size: 1.1em;
	font-weight: bold;
	font-style: italic;
	line-height: 1.5em;
	font-variant: uppercase;
}

All these properties can be condensed into a one row expression using this syntax:

font: fontSize/lineHeight weight style variant family;

See the example below:

.myClass{
	font: 1.1em/1.5em bold italic uppercase Arial,Verdana,Helvetica,sans-serif;
}

Just remember that this syntax will only function if you specify both font-size and font-family.

Remove leading zeroes in XSL

A useful XSL template for removing leading zeros

<xsl:template name="removeLeadingZeros">  
  <xsl:param name="originalString"/>  
  <xsl:choose>  
    <xsl:when test="starts-with($originalString,'0')">  
      <xsl:call-template name="removeLeadingZeros">  
        <xsl:with-param name="originalString">  
          <xsl:value-of select="substring-after($originalString,'0' )"/>  
        </xsl:with-param>  
      </xsl:call-template>  
    </xsl:when>  
    <xsl:otherwise>  
      <xsl:value-of select="$originalString"/>  
    </xsl:otherwise>  
  </xsl:choose>  
</xsl:template>

Just insert it into your XSL file and use it as usual:

<xsl:call-template name="removeLeadingZeros">  
  <xsl:with-param name="originalString" select="$myOriginalString"/>  
</xsl:call-template>

Zipped folder backup

A simple bash script that zips a folder and sets the archive name to the current date.

#!/bin/bash

# Archive name structure: ddMMYYYY_HHmmss.zip

archiveName=`date +%d%m%Y_%H%M%S`.zip

folderName=MyFolder

zip -r $archiveName $folderName

Install OpenOffice 3.1 on Ubuntu

These are the steps to install OpenOffice 3.1 on Ubuntu. First of all download OpenOffice 3.1 from the official web site. When you’re done, follow these steps:

1. Remove previous version

sudo apt-get remove openoffice*.*

2. Remove settings folder This will remove ALL your OpenOffice settings

rm ~/.openoffice.org -rf

3. Expand the archive

tar -xvzf OOo_3.1.0_LinuxIntel_install_en-US_deb.tar.gz

4. Setup

cd OOO310_m11_native_packed-4_en-US.9399/DEBS/

sudo dpkg -i *.deb

5. Setup desktop integration

cd desktop-integration

sudo dpkg -i openoffice.org3.1-debian-menus_3.1-9393_all.deb

Java tail

A java implementation of the Unix tail command. This is a small customization of the java tail class created by Luigi Viggiano. I’ve just added some controls and added the update time parameter.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
 
/**
 * Java implementation of the Unix tail command
 * 
 * @param args[0] File name
 * @param args[1] Update time (seconds). Optional. Default value is 1 second
 * 
 * @author Luigi Viggiano (original author) http://it.newinstance.it/2005/11/19/listening-changes-on-a-text-file-unix-tail-implementation-with-java/
 * @author Alessandro Melandri (modified by)
 * */
public class Tail {
 
  static long sleepTime = 1000;
 
  public static void main(String[] args) throws IOException {
 
    if (args.length > 0){
 
      if (args.length > 1)
        sleepTime = Long.parseLong(args[1]) * 1000;
 
      BufferedReader input = new BufferedReader(new FileReader(args[0]));
      String currentLine = null;
 
      while (true) {
 
        if ((currentLine = input.readLine()) != null) {
          System.out.println(currentLine);
          continue;
        }
 
        try {
          Thread.sleep(sleepTime);
        } catch (InterruptedException e) {
          Thread.currentThread().interrupt();
          break;
        }
 
      }
      input.close();
 
    } else {
      System.out.println("Missing parameter!\nUsage: java JavaTail fileName [updateTime (Seconds. default to 1 second)]");
    }
  }
}

This is also a Gist. Check it out!

VMWare and Linux

If you install VMware on linux you may find that some keys (up/down keys, home key, etc…) don’t work in the guest operating system. You need to create a config file inside the .vmware folder in your home directory and paste this text info the file:

xkeymap.keycode.108 = 0x138 # Alt_R
xkeymap.keycode.106 = 0x135 # KP_Divide
xkeymap.keycode.104 = 0x11c # KP_Enter
xkeymap.keycode.111 = 0x148 # Up
xkeymap.keycode.116 = 0x150 # Down
xkeymap.keycode.113 = 0x14b # Left
xkeymap.keycode.114 = 0x14d # Right
xkeymap.keycode.105 = 0x11d # Control_R
xkeymap.keycode.118 = 0x152 # Insert
xkeymap.keycode.119 = 0x153 # Delete
xkeymap.keycode.110 = 0x147 # Home
xkeymap.keycode.115 = 0x14f # End
xkeymap.keycode.112 = 0x149 # Prior
xkeymap.keycode.117 = 0x151 # Next
xkeymap.keycode.78 = 0x46 # Scroll_Lock
xkeymap.keycode.127 = 0x100 # Pause
xkeymap.keycode.133 = 0x15b # Meta_L
xkeymap.keycode.134 = 0x15c # Meta_R
xkeymap.keycode.135 = 0x15d # Menu

Install Sun JDK on Linux

I’ve tested this procedure on Fedora 10 and Ubuntu 9.04 but it should work on other distributions too.

First of all download the latest JDK package from the SUN page: be sure to download the bin file and not the rpm.

From now on you’ll need to run commands using sudo.

Move the package to /opt/ and make it executable.

cd /path/to/download/folder

mv jdk-versionnumber-linux-i586.bin /opt/

cd /opt/

chmod +x jdk-versionnumber-linux-i586.bin

Now start the installation and follow the onscreen instructions

./jdk-versionnumber-linux-i586.bin

When the installation is done you’ll need to set the JAVA_HOME enviroment variable and add java executable to the system pah.

Open the file /etc/profile and add the following lines

JAVA_HOME="/opt/jdk_versionnumber"

export JAVA_HOME

PATH=$PATH:/opt/jdk_versionnumber/bin/

export PATH

Now add a symbolic link for the java command

ln -s /opt/jdk_versionnumber/bin/java /usr/bin/java

Log off and log in back and you’re done.