Thursday, 17 October 2013

Step by Step Guide on How to Install Java on Linux [with Pictures]


Java is the most basic requirement for any computer to have no matter what operating system it is running. Same goes for linux as well. However linux is not so user-friendly and can be brutal to newbies. Well we've taken care of that because with this guide atleast installing java will more of a learning experience than headache. Follow this hassle-free, step-by-step guide to installing java on linux and learn a few useful commands on the way. 



Courtesy: Steve Winton, Flickr



Also See:



Whether you need to run an application or develop one, Oracle Java is must have for any system and that goes same for your Linux machine as well. With that being said, by the end of this easy, step-by-step tutorial you will have Oracle Java fully installed and configured for your Linux machine be it Ubuntu, Debian or Mint. Please note that for the duration of this tutorial i will be using Ubuntu 14.04. Further, where ever possible along with the steps that make use of the terminal, i will also mention alternative methods to execute those same steps, the non-terminal / coding way to help our newbie friends. 


Now, you are going to be installing either Oracle JDK or Oracle JRE so it's important to understand the difference between both of them.

Oracle Java Runtime Environment, JRE is the minimum requirement for executing a Java program. It consists of the Java Virtual Machine (JVM), core classes, and supporting files. In short, when you only care about running Java programs on your browser or computer, you will install JRE. 





Oracle Java Development Kit, JDK is a Software Development Kit for Java which includes the Java Runtime Environment, the JVM and all of the class libraries present in the production environment to create and compile programs so if you are planning to do some Java development, you will need JDK.






1. Checking Java Version

To check java version simply open terminal and type:  java -version

1.1. If you don't have java installed in your system, you will see a message as shown below. In such case straight away jump to step 3 and follow the instructions.



1.2. If you have Open Java installed, then you'll see a message similar to the one shown below. If you have Open Java installed, strrightaskip to step 2 and follow the instructions.





1.3. If you've already got Oracle Java installed, then you will see a message similar to the one shown below. In such case, it is recommended to compare your existing java version with the latest one as mentioned on the official website and updat accordingly using the following guide.









2. Removing Open JDK 

2.1. In Linux, by default Open JDK is made available which is quite unstable and buggy and hence not recommended for enterprise / commercial application development. As compared to Open JDK, performance and stability is noticeably better on Oracle Java which is why you should remove Open JDK and replace it with Oracle JDK / JRE. 

Non-Terminal Method:
Ubuntu users can simply open the Ubuntu Software Centre, search "OpenJDK", select the version you installed and click on Remove as depicted in the screenshot below.






Alternatively,


Terminal Method:
You can also remove OpenJDK by typing the following command in the terminal and pressing enter:


sudo apt-get purge openjdk-\*




When asked whether you want to continue or not, type "y" and press enter.








3. Creating the Java folder

We will now be creating an empty folder named "Java" for holding your JDK/JRE files in the /usr/local directory making the folder's path as  /usr/local/java. Type the following command in the terminal and press Enter:


sudo mkdir -p /usr/local/java






4. Checking Linux Version

Before downloading the java files we need to know what version of Linux (32 bit or 64 bit) are we running. 

Terminal Method:
Go to the terminal, type the following command and press enter:


file /sbin/init


For example, you can see in the screenshot above that result obtained prior to the command execution tells me that i am running 64 bit Linux.



Alternatively,


Non-Terminal Method:
1.1. Ubuntu users can check the version by going to System Settings and under System panel, select Details.






1.2. Dialog box named Details will open where in the Overview tab, besides OS type will be Linux Version is written as shown in the screenshot.





4.2. Dialog box named Details will open where in the Overview tab, besides OS type will be Linux Version is written as shown in the screenshot.








5. Downloading Oracle JDK / JRE

5.1. Go to the Java Download Page and based on your requirement select either JDK or JRE . As mentioned above that when you only care about running Java programs on your browser or computer you should install JRE whereas JDK is a full featured Software Development Kit for Java, including JRE and is useful when you plan to do some java application development such as android development etc.





If you select Java JDK then you will be redirected to the screen shown below.
Under Java SE Development Kit, Click on Accept License Agreement.

Select the Linux x86 link if you are running 32 bit Linux.
Select the Linux x64 link if you are running 64 bit Linux.

The file should get downloaded to home/"user_name"/Downloads directory. For example: in my case it is home/kross/Downloads where kross is the username.





If you select Java JRE then you will be redirected to the screen shown below.
Under Java SE Runtime Environment, Click on Accept License Agreement.

Select the Linux x86 link if you are running 32 bit Linux.
Select the Linux x64 link if you are running 64 bit Linux.

The file should get downloaded to home/"user_name"/Downloads directory. For example: in my case it is home/kross/Downloads where kross is the username.









6. Copying the tar.gz JRE / JDK files to the /usr/local/java directory

In most cases your downloaded JDK / JRE tar.gz files will be downloaded to the home/"user_name"/Downloads directory. For example: in my case it is home/kross/Downloads where kross is the username.

NOTE: 
You can see that the downloaded files are by default named in a specific way such as jdk-8u5-linux-i586.tar.gz which in this case is a 32 bit JDK version 8u5 file. In the future, when newer version of java will get released such as 9u45, in such as case do a simple replacement as shown below:

1. For 32 bit JDK file:  jdk-9u45-linux-i586.tar.gz 

2. For 32 bit JRE file:   jre-9u45-linux-i586.tar.gz 
3. For 64 bit JDK file:  jdk-9u45-linux-x64.tar.gz
4. For 64 bit JRE file:  jre-9u45-linux-x64.tar.gz

Similarly,

9u45 is equivalent to 1.9.0_45
8u5 is equivalent to 1.8.0_05
8u0 is equivalent to 1.8.0_00



Copying 32-bit Oracle Java JDK in 32-bit Linux:

6.1.1. Type and press enter : cd ~/Downloads

6.1.2. Now if you downloaded the 32 bit Oracle JDK then type and press enter : 
          sudo cp -r jdk-8u5-linux-i586.tar.gz /usr/local/java  
            



Copying 32-bit Oracle Java JRE in 32-bit Linux:

 6.2.1. Type and press enter : cd ~/Downloads

 6.2.2. However if you downloaded the 32 bit Oracle JRE then type and press enter : 
          sudo cp -r jre-8u5-linux-i586.tar.gz /usr/local/java





Copying 64-bit Oracle Java JDK in 64-bit Linux:

6.3.1 Type and press enter : cd ~/Downloads

6.3.2. Now if you downloaded the 64 bit Oracle JDK then type and press enter : 
          sudo cp -r jdk-8u5-linux-x64.tar.gz /usr/local/java



         


Copying 64-bit Oracle Java JRE in 64-bit Linux:

6.4.1. Type and press enter : cd ~/Downloads

6.4.2. If you downloaded the 64 bit Oracle JRE then type and press enter : 
          sudo cp -r jre-8u5-linux-x64.tar.gz /usr/local/java







7. Extracting the tar.gz JRE / JDK files to the usr/local/java directory


Extracting 32-bit Oracle Java JDK in 32-bit Linux:

7.1.1. Type and press enter: cd /usr/local/java

7.1.2. If you downloaded the 32 bit Oracle JDK then type and press enter : 
           sudo tar xvzf jdk-8u5-linux-i586.tar.gz 




          
Extracting 32-bit Oracle Java JRE in 32-bit Linux:

7.2.1. Type and press enter: cd /usr/local/java

7.2.2. If you downloaded the 32 bit Oracle JRE then type and press enter : 
           sudo tar xvzf jre-8u5-linux-i586.tar.gz 




Extracting 64-bit Oracle Java JDK in 64-bit Linux:

7.3.1. Type and press enter: cd /usr/local/java


7.3.2. If you downloaded the 64 bit Oracle JDK then type and press enter : 
           sudo tar xvzf jdk-8u5-linux-x64.tar.gz





          
Extracting 64-bit Oracle Java JRE in 64-bit Linux:

7.4.1. Type and press enter: cd /usr/local/java

7.4.2. If you downloaded the 64 bit Oracle JRE then type and press enter : 
            sudo tar xvzf jre-8u5-linux-x64.tar.gz 







8. Editing the system Path file /etc/profile.

8.1. Open terminal and type and press enter: sudo gedit /etc/profile
This is opens up a text editor which contains all Linux system wide environment and startup programs.

8.2. Scroll down to the end of the file and add the following lines below to the end of your /etc/profile file:

If you are installing JDK then Copy and Paste the following code:
JAVA_HOME=/usr/local/java/jdk1.8.0_05
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
JRE_HOME=/usr/local/java/jdk1.7.0_05/jre
PATH=$PATH:$HOME/bin:$JRE_HOME/bin
export JAVA_HOME
export JRE_HOME
export PATH





If you are installing JRE then Copy and Paste the following code:
JRE_HOME=/usr/local/java/jre1.8.0_05
PATH=$PATH:$HOME/bin:$JRE_HOME/bin
export JRE_HOME
export PATH





8.3. Save the /etc/profile file and exit.





9. Informing Linux about your Oracle Java's location.

The following commands notify the system that Oracle Java JDK / JRE is available for use.

For 32 / 64 bit Oracle JDK

9.1.1. If you are installing JDK then type and press enter:
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/jdk1.8.0_05/jre/bin/java" 1

9.1.2. Type and press enter:
 sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/local/java/jdk1.8.0_05/bin/javac" 1

9.1.3. Type and press enter:
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/local/java/jdk1.8.0_05/bin/javaws" 1






For 32 / 64 bit Oracle JRE


9.2.1. If you are installing JDK then type and press enter:
 sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/jre1.8.0_05/bin/java" 1

9.2.2. Type and press enter:
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/local/java/jre1.8.0_05/bin/javaws" 1









10.  Making Oracle Java JDK/JRE as the default Java.


For 32 / 64 bit Oracle JDK

10.1.1. If you are installing JDK then type and press enter:
sudo update-alternatives --set java /usr/local/java/jdk1.8.0_05/jre/bin/java

10.1.2. If you are installing the JDK then type and press enter:
sudo update-alternatives --set javac /usr/local/java/jdk1.8.0_05/bin/javac

10.1.3. If you are installing JDK then type and press enter:
sudo update-alternatives --set javaws /usr/local/java/jdk1.8.0_05/bin/javaws





For 32 / 64 bit Oracle JRE

10.2.1. If you are installing JRE then type and press enter:
sudo update-alternatives --set java /usr/local/java/jre1.8.0_05/bin/java
       
10.2.2. If you are installing JDK then type and press enter:
sudo update-alternatives --set javaws /usr/local/java/jre1.8.0_05/bin/javaws
         







11. Reloading the system Path file /etc/profile.

Type and press enter: . /etc/profile
NOTE: The system-wide Path file /etc/profile file will reload after reboot of your Linux system.






12. Testing a successful installation

Open terminal, type and press enter: java -version
Successful installation of 32 bit java will show:



However, successful installation of 64 bit java will show:



If the above mentioned tests work out the way as depicted above then you have successfully installed java on your linux system. Blast your questions, doubts, suggestions or thank you's (which would be nice..) in the comment section. I would be more then happy to help you.

- Kaushal Kapoor



Also See: