set JAVA_HOME ubuntu

How to Set JAVA_HOME in Ubuntu

Java is a popular programming language used for software and web development by many developers and organizations. However, almost every Java-based program, or library will look for the home directory of Java installation, every time it runs. So it is advisable to set the path to Java installation as an environment variable JAVA_HOME so that you don’t have to specify it every single time. In this article, we will learn how to set JAVA_HOME in Ubuntu. You can use these steps for other Linus distributions also.


How to Set JAVA_HOME in Ubuntu

Here are the steps to configure JAVA_HOME in Ubuntu.


1. Find Location of Java

First of all, we need to find out the exact location of our Java installation. Open terminal and run the following command to find it.

$ which java

The output will be something like the following. The number 1.7.0 will be different, depending on the Java version installed on your system.

/usr/lib/jvm/jdk1.7.0
OR
/usr/java/jdk1.5.0_07/bin


2. Open Profile File

If you want to set JAVA_HOME for single user open terminal and run the following command to open bash_profile file.

$ vi ~/.bash_profile

If you want to set JAVA_HOME for all users on your system, open terminal and run the following command.

$ vi /etc/profile


3. Set JAVA_HOME

Add the following lines to your file.

JAVA_HOME=/usr/lib/jvm/jdk1.7.0
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin

The first line sets JAVA_HOME variable to the installation folder of Java binary. The second line appends this path to the PATH environment variable so that it can be easily accessed from any location on your system.

Save and close the file.


4. Apply changes

Run the following command to apply changes. If you have configured JAVA_HOME for just one user run the following command.

$ source ~/.bash_profile

If you have configured JAVA_HOME for all users run the following command.

$ source /etc/profile

Basically, you have mention the path of the file you opened in step 2, after the source keyword.

That’s it. Now, you will find that the JAVA_HOME environment variable has been set in Ubuntu.

Also read:

How to Create Large File in Linux
How to Find JDK Path in Ubuntu
How to Get Length of List in Django template
Difference Between Df and Du command
How to Create Startup Disk for Ubuntu

Leave a Reply

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