Sunday, April 12, 2015

Install and configure Java in ubuntu easy

Whenever I change laptop or format my hard drive, I end up making a mess with the soft links to configure java...

This post is just a reminder too myself, for the future. I found a clean way I am comfortable with for setting my system to use java, via the update-alternatives tool that ubuntu has. This is how I do it:

1- Download the JDK from oracle and copy it to your user directory.
I like having it under ~/Java/jdk1.8.0_40

2- I often like installing maven, so I do the same, just I create another directory for it and I put in there the downloaded maven. ~/maven/apache-maven3.3.1

3- Then I set the necessary environmental variables for my user in the ~/.bashrc file.

export JAVA_HOME=/home/djordje/Java/jdk1.8.0_40
export M2_HOME=/home/djordje/maven/apache-maven-3.3.1
export M2=$M2_HOME/bin
export PATH=$PATH:$JAVA_HOME/bin:$M2  

4- I use update-alternatives to install in the system the java interpreter and the compiler.

sudo update-alternatives --install "/usr/bin/java" "java" "/home/djordje/Java/jdk1.8.0_40/bin/java" 1

sudo update-alternatives --install "/usr/bin/javac" "javac" "/home/djordje/Java/jdk1.8.0_40/bin/javac" 1

5- Again i use update-alternatives, but for this time to configure the default version of the interpreter and compiler to be used by default.

sudo update-alternatives --config java
sudo update-alternatives --config javac

When running the command, it shows all versions of java installed, to pick one

6- At the end I just run java -version to see that the system is using the version of java I want.

I like this way, it is easy to do and you can quickly switch to a different installed version by just using the --config option

1 comment:

Share with your friends