As a Java developer, when installing a new Ubuntu development system, you're sometimes left with various Java tools of various flavours. Like, for instance,
gcj.
Not only that - Debian-based systems (of which Ubuntu is one) uses the alternatives system to enable you to link the
javac command to various implementations of the compiler (of which afore-mentioned gcj is one; others include Sun's own compiler, and the Eclipse compiler). Since an explanation of the alternatives system is out-of-scope for this post, you can read about it here:
Debian alternatives system.
So, here lies the problem: like me, you probably want to know exactly what's on your machine. You also don't want to mix your tools, e.g. compile with
gcj, but then run the binary with Sun's
java (would this even work?).
However powerful the alternatives system can be, it's a bit too... INTRUSIVE for a newcomer
*
Let's do some cleaning up by removing the alternative installations to all our favourite tools, then tell the OS to use our own tools. (This was tested on Ubuntu 8.04)
Firstly, start by downloading your tools from the web, e.g. JDK, Maven, Ant, etc. While the tools are downloading, let's do some cleaning up.
(You may want to read up on how the alternatives system actually work before blindly running my scripts:
alternatives how-to)
Now, you can decide to completely remove Ubuntu-maintained tools from your machine by removing them with Synaptic package manager. Or you can just install the new tools, and tell the alternatives system to prefer your tools above the others. I'll follow the former method, since it frees up a bit of valuable space too.
(PS: the below is just a sample of things that may or may not be installed on your machine. Use Synaptic Package Manager instead, and search for "java" and remove accordingly)
sudo aptitude remove -y sun-java6-jdk sun-java6-jre sun-java6-bin sun-java6-plugin
You may need to completely remove the historical alternatives:
update-alternatives --remove-all java
!!:gs/java/jar/
!!:gs/jar/javac/
!!:gs/javac/javah/
!!:gs/javah/javap/
!!:gs/javap/javaws/
!!:gs/javaws/jconsole/
!!:gs/jconsole/jhat/
!!:gs/jhat/jinfo/
!!:gs/jinfo/jmap/
!!:gs/jmap/jps/
!!:gs/jps/jvisualvm/
As you can see, not only do I use
java and
javac, but also
jhat,
jvisualvm, etc.
The
!!:gs/A/B/ command basically means "repeat the previous command, but replace A with B, all occurrences", as opposed to the
^A^B command which will only replace the first occurrence.
(There's some really powerful Linux commands out there, and here's some cool ones:
commandlinefu.com)
Install your new JDK, set up it's environment variable in /etc/profile (e.g. JAVA_HOME=/opt/jdk/jdk1.6.0_14), and have your current shell load the variable with:
source /etc/profile
Tell the alternatives system where it can find the new utilities:
update-alternatives --install /usr/bin/java java ${JAVA_HOME}/bin/java 50
!!:gs/java/jar/
!!:gs/jar/javac/
!!:gs/javac/javah/
!!:gs/javah/javap/
!!:gs/javap/javaws/
!!:gs/javaws/jconsole/
!!:gs/jconsole/jhat/
!!:gs/jhat/jinfo/
!!:gs/jinfo/jmap/
!!:gs/jmap/jps/
!!:gs/jps/jvisualvm/
You can now try one of the above commands on the command line, and it will work:
$ java -version
java version "1.6.0_14"
Java(TM) SE Runtime Environment (build 1.6.0_14-b08)
Java HotSpot(TM) Server VM (build 14.0-b16, mixed mode)
The same can now be done for your other tools, like Ant and Maven.
Happy coding
* Image attributed to http://www.flickr.com/photos/erica_marshall/3017362565/#DiscussPhoto under the Creative Commons License: http://creativecommons.org/licenses/by-nc-sa/3.0/