I know lot of you starting out new as Linux users or Java learners on Linux platform find it an issue to install JDK or may be configure it to start using after installing. At time the version of java that comes with the package manager (apt-get for Ubuntu) would be an older version and you would require you to download the compressed binaries. In any case I have outlined how to configure in both the cases.
Case: 1- You would have to download the required JDK binary package from here. And follow the Steps for Case-1 below. This would involve extracting the compressed binary and then setting up the $PATH.
Case: 2- You have installed the JDK package using the apt-get and it installs JDK to some location (I haven’t used this approach because when ever I tried I ended up with an older version of JDK, so I have not explained it in detail).
Also note that usually the Linux installation might come with the OpenJDK but I have never used that before.
Steps for Case-1
Suppose you happen to download the JDK from the Oracle Downloads site here, then you can follow the below procedure:
1. Download the required Java SE package from the Oracle Download site here. [Download the compressed Binary and Preferably Java SE 7]
2. Extract it in to a location of you choice- I used the /home/jdk<version>
3. Update the $PATH variable to locate the jdk/bin directory so that the shell can recognize the java and javaccommands. This can be done by appending the PATH variable with the location to the jdk bin. In bash you can either use a seperate alias file- .bash_aliases and refer it in the .bashrc file or directly add in the .bashrc file. Here I am using a seperate alias file: .bash_aliases - In this file u can specify the variable values- for PATH, CLASSPATH and others required ones and also aliases for you various commands.
There’s a catch here: If you just initialize the $PATH variable with the jdk bin location then you will loose out the access to other bin dirs like- /usr/bin, /bin, /usr/local/bin and so on. Instead you would have to update/append the $PATH variable.
Lets assume you extracted the JDK compressed binary to- /home/jdk1.6.0_16
(Note: the dir- jdk1.6.0_16 naming would differ based on the version of JDK you have downloaded)
So the .bash_aliases file would look something like this
JAVA_HOME=/home/usr/jdk1.6.0_16 export JAVA_HOME PATH=$PATH:$JAVA_HOME/bin: export PATH CLASSPATH=$JAVA_HOME/lib/:. export CLASSPATH
The ‘:‘ is the separator, for windows it would be “;“. Its optional to update the CLASSPATH as one can provide it during the execution of Java program or if you are using an IDE it will manage the CLASSPATH or for that matter if you are using an tools like Ant, Maven. So just in case you are used to providing the CLASSPATH and may be use command line at times for you demo programs here’s a small tip:
Note that in the CLASSPATH- there’s an addition value “.“. This points to the current directory and will be helpful when the class files are in the current directory. Otherwise java will search for the classes in the directories mentioned in the CLASSPATH and will return ClassNotFoundException if the current directory is not part of the CLASSPATH and the required class files are in the current directory. Other option is to use -cpand specify the classpath. In that case the value of the CLASSPATH variable is overridden with the value specified by -cp.
Steps for Case-2
Suppose you have use the apt-get command to install the JDK, the you can follow the below procedure and this has to be done every time you enter the terminal or best is to put it in a .bashrc or .bash_aliases file as mentioned above:
JAVA_HOME=/usr/lib/jvm/java export JAVA_HOME PATH=$PATH:$JAVA_HOME/bin export PATH