How to Set JAVA_HOME Environment Variable in Linux

terminal

Googling around the Internet, one may find complicated steps to enable the JAVA_HOME environment variable in Linux. To be honest, it’s not really that complicated, but it just needs a little bit more explaining. Here’s how to set JAVA_HOME in Linux for two aspects:

If you would like to know how to setup JAVA JDK in Linux, refer here: Installing Oracle Java JDK 6 or 7 on Ubuntu



Identifying the Location of the JAVA Installation

Open up the terminal, and locate the location of java:

whereis java
java: /usr/bin/java /etc/java /usr/bin/X11/java /usr/share/java

Note that what you’re looking for /usr/bin/javafor this guide.Continue in the terminal to find the absolute path of java (since the above is just a symbolic link):

ls -lah /usr/bin/java
lrwxrwxrwx 1 root root 22 May  9 13:32 /usr/bin/java -> /etc/alternatives/java

ls -lah /etc/alternatives/java
lrwxrwxrwx 1 root root 33 May 23 16:40 /etc/alternatives/java -> /usr/lib/jvm/jdk1.6.0_32/bin/java

Based on the result above, we can see a couple of things:

  • /usr/bin/java is a symbolic link to /etc/alternatives/java
  • /etc/alternatives/java is a symbolic link to /usr/lib/jvm/jdk1.6.0_32/bin/java

What we have is that /usr/lib/jvm/jdk1.6.0_32/bin/java is the absolute path to java.


Setting JAVA_HOME for a Single User

  1. Locate absolute path to the JAVA installation.
  2. Open up the .bash_profile in your terminal using vi:
    vi ~/.bash_profile
    
  3. Set the JAVA_HOME environment variable using the following syntax and using the path you got from the above method:
    export JAVA_HOME=/usr/lib/jvm/jdk1.6.0_32
    
  4. Now, it’s time to set the PATH variable. It’s the same path as the one you set for JAVA_HOME; only excluding the java console:
    export PATH=$PATH:/usr/lib/jvm/jdk1.6.0_32/bin
    
  5. Now, all you have to do is logout and login back to the session for JAVA_HOME to take effect! Alternatively, if you would prefer the changes to take effect immediately, just type the following in the terminal:
    source ~/.bash_profile
    

    or:

    . ~/.bash_profile
    


Setting JAVA_HOME for All Users

  1. Locate absolute path to the JAVA installation.
  2. Open up the profile in your terminal using vi:
    sudo vi /etc/profile
    
  3. Set the JAVA_HOME environment variable using the following syntax:
    export JAVA_HOME=/usr/lib/jvm/jdk1.6.0_32
    
  4. Don’t forget to set the PATH variable too:
    export PATH=$PATH:/usr/lib/jvm/jdk1.6.0_32/bin
    
  5. You can now logout and login back to the session for the settings you’ve made to take effect immediately, or just type the following for an immediate effect:
    source /etc/profile
    

    or:

    . /etc/profile
    

Was That a Good Read? Rate It!

1 Star2 Stars3 Stars4 Stars5 Stars (9 votes, average: 4.89 out of 5)
Loading ... Loading ...

6 Comments

Add a Comment

Your email address will not be published. Required fields are marked *

Current day month ye@r *

CommentLuv badge