First you should be aware that there is no Java installed on a freshly intalled Ubuntu 14.04 LTS. This can be verified easily by executing 'java -version' in a terminal. This article assumes that there is no Java installed.
Why Oracle Java? Why not OpenJDK? Good question and the answer is that it is recommended by Google. Should you plan to use Java for Android programming later, then Google says it is better to use Oracle's version - their Android Studio says that there are stability issues with OpenJDK. Whether this is true is a moot point here - if you do plan to do Android programming later, then install Oracle's Java - else, any Java will do, even OpenJDK.
Here are the steps for installing Oracle JDK 8. Note that a web browser is used to download the JDK - not Ubuntu Software Center or Synaptic.
- Point your browser to here and click on the JDK download button (not the Server JRE or JRE). On the page that appears next, click to 'Accept License Agreement' and select either the 32-bit or 64-bit tar.gz file to start the download. Save the file and wait for the download to finish. If you have not changed anything, the JDK file will be found in your Home folder Downloads location.
- On the same web page, scroll down and click on either the 32-bit or 64-bit Demo and Sample file for downloading. Again, save the file and wait for the download to finish,
- Use the 'Back' key for your browser to go back one page and scroll down until you see Java SE 8 Documentation and click on the download button. On the next page, accept the license agreement and click on download the zip file for the documentation for the JDK (not the JavaFX). Save the file and wait for the download to finish.
Then extract the contents of the 'jdk-8u25-linux-x64.tar.gz' file by executing:
tar xzvf jdk-8u25-linux-x64.tar.gz
This will create a 'jdk1.8.0_25' directory in the Downloads directory while displaying a lot of stuff on the terminal's screen. After this is completed and you are back to the command prompt, you can extract the demos and samples by executing:
tar xzvf jdk-8u25-linux-x64-demos.tar.gz
This will create 2 directories under the 'jdk1.8.0_25' directory - 'sample' and 'demo' directories. When completed, you can now extract the Java API documents by executing:
unzip jdk-8u25-docs-all.zip -d jdk1.8.0_25
This will create a 'doc' directory under the 'jdk1.8.0_25' directory. After this is completed, you now have all the files necessary for the Oracle Java JDK 8 - all you have to do is to put them in the proper place.
The 'proper place' can be anywhere - your home directory (if you want Java for your own use only) or '/usr/local/' or 'usr/lib/jvm', etc. Note that if you want Java to reside as a sub-directory in '/usr/local/' or '/usr/lib/jvm/', then you need to use 'sudo' since administrator privileges are needed to create sub-directories there. This is not required if you install Java in your home directory. For this article, we will use '/usr/lib/jvm/' directory as this is where all Java related files end up if it was installed by default.
First we have to create the /usr/lib/jvm directory by executing:
sudo mkdir /usr/lib/jvm
Next, we move the 'jdk1.8.0_25' directory to /usr/lib/jvm
sudo mv ~/Downloads/jdk1.8.0_25/ /usr/lib/jvm/
The directory will be moved from your home Downloads directory to the '/usr/lib/jvm/' directory. You can verify this by listing the contents of the 'usr/lib/jvm/' directory using the 'ls -la' command. Note that the owner and group for the jdk1.8.0_25 directory is yours but you can easily change this by executing:
sudo chown -R root:root /usr/lib/jvm/jdk1.8.0_25
Again, you can verify this by using the 'ls -la' command.
The next thing to do is to tell Ubuntu where Oracle Java JDK resides. For this we use the 'update-alternatives' command. Execute the following commands one by one from the terminal (note that all commands are on a single line):
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.8.0_25/bin/java" 0
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.8.0_25/bin/javac" 0
These will install all the necessary links to Oracle's Java. Next we have to tell Ubuntu the default Java to use. Execute the following commands one by one from the terminal (note that all commands are on a single line):
sudo update-alternatives --set java /usr/lib/jvm/jdk1.8.0_25/bin/java
sudo update-alternatives --set javac /usr/lib/jvm/jdk1.8.0_25/bin/javac
To verify that all have been set correctly, you can use the following commands:
update-alternatives --list java
update-alternatives --list javac
If the output to the 2 queries above are /usr/lib/jvm/jdk1.8.0_25/bin/java and /usr/lib/jvm/jdk1.8.0_25/bin/javac, then everything have been set correctly. Now that you have informed Ubuntu about Oracle's Java, you still have to perform one last task before Java is set up and ready for use. This is to set the environment variables and paths correctly for the whole system. Execute the following command:
sudo nano /etc/profile
This will execute the nano text editor and open the /etc/profile file for editing. Now add the following lines at the end of the file.
Save the file and close it. Note that, upon system startup, these will set the JAVA_HOME and PATH environment variables for the whole system. The JAVA_HOME environment variable points to the JDK root while the PATH environment variable will add the /bin directory of the JDK to whatever path that has already been specified. This will enable the 'javac' command to be executed from any directory.
Now log out and then log in again to the Ubuntu system in order to make sure that both the environment variables is set. Check by opening a terminal and executing:
Then as the last check, execute the following:
If you see 'java version "1.8.0_25"' and 'javac 1.8.0_25' in the output, then you are now ready to use Oracle's Java on your Ubuntu system. You can also delete the tar.gz and zip files which were downloaded from the web in order to free up disk space.
Where to go from here? If you are familiar with Java programming, you can go ahead to use Java anyway you like it. If you are a novice and this is the first time you are using Java, then I would recommend searching the web for tutorials and the books "Java For Dummies, 6th Edition" and "Head First Java, 2nd Edition" as a guide. Happy Computing!