Monday, June 30, 2014

How to Install/Setup Java Path on Linux

Method 1:

If we do  not require any particular version of Java, then by default running the following command at terminal would result in default  open-jdk Java installation .

On Ubuntu
$ sudo apt-get install openjdk-7-jre
On  Redhat/CentOs/Fedora

   $ su -c "yum install java-1.7.0-openjdk"

Agree to install and java should be up and running after the installation completes.

Method 2 :

In some cases, if we require some specific version of java (Eg, Oracle )  due to some business requirements.
Then we need to manually download the binary , install and the set the path for applications to recognize java.

Download the binary from the respective website, here I am using oracle jdk 7 for linux x64 bit PC.

For example the file was  downloaded to /home/vipul/Downloads

I used the following link to download the setup.

Java SE Development Kit 7 Downloads

Now extract the file using the following command

$ tar -xzvf  jdk-7u60-linux-x64.tar.gz

Which would result in extracted directory containing the setup.

Now move the extracted folder(dir) to the path of your choice and here I have renamed the folder
jdk1.7.0_60 to java using the following command

$ mv jdk1.7.0_60 java

Next move java to desired location

$ mv java /home/vipul

Now lets , set the path for Java.

To Set JAVA_HOME / PATH for a single user, Login to your account and open .bashrc file

$ vi ~/.bashrc
Set JAVA_HOME as follows using syntax export JAVA_HOME=<path-to-java>.

export JAVA_HOME=/home/vipul/java

Set PATH as follows:

export PATH=$PATH:$JAVA_HOME/bin

Please replace the path according to your java home directory .
Log out or restart for new settings to show.
Alternatively, type the following command to activate the new path settings immediately:

$ source ~/.bashrc

Verify new settings:

$ echo $JAVA_HOME
$ echo $PATH
Tip: Use the following command to find out exact path to which java executable under UNIX / Linux:

$ which java

Please note that the file ~/.bashrc is similar, with the exception that ~/.bash_profile runs only for Bash login shells and .bashrc runs for every new Bash shell.

To Set JAVA_HOME / PATH for all user, We need  to setup global config in /etc/profile OR /etc/bash.bashrc file for all users:

# vi /etc/profile
Next setup PATH / JAVA_PATH variables as follows:

export JAVA_HOME=/home/vipul/java
export PATH=$PATH:$JAVA_HOME/bin
Save and close the file. Once again you need to type the following command to activate the path settings immediately:

# source /etc/profile

Next : Create and Setup SSH Certificates

For detailed reading , please refer the following WikiHow blog

Install-Oracle-Java-on-Ubuntu-Linux

No comments:

Post a Comment