How to install Ant correctly on Ubuntu

When using Ant the first time, you might notice that it return errors

ant -version
Unable to locate tools.jar. Expected to find it in /usr/lib/jvm/java-6-openjdk/lib/tools.jar

Is it because of wrong jdk version? You probably tried to locate tools.jar and found this:

locate tools.jar
/usr/lib/jvm/java-6-sun-1.6.0.06/lib/tools.jar

Is it the same tools.jar required by ant? How can you point to this file so ant will not return any error? Here is the solution:

  1. Make sure you have the “multiverse” repository
  2. Install all Sun Java 6 packages
sudo apt-get clean
sudo apt-get update
sudo apt-get install sun-java6*

This process might take a while, so you might need to grab a cup of coffee and wait.

  1. Check that you are using the newly installed jdk
javac -version

The output should be something like this:

javac 1.6.0_06
  1. Check that you are using the correct jre
java -version

The output should be something likeā€¦

java version "1.6.0_06"
Java(TM) SE Runtime Environment (build 1.6.0_06-b02)
Java HotSpot(TM) Client VM (build 10.0-b22, mixed mode, sharing)

If you find you are not using the correct Sun Java 6 jre you can fix it by:

sudo update-alternatives --config java

The above command will let you choose which jre to make default if you have multiple jre installed.