How to Set JAVA_HOME / PATH variables Under Linux Bash Profile

Q. I just need a help to show me how to setup java path on Linux. How can I set JAVA_HOME and PATH variables for every user under my Linux system?

A. ~/.bash_profile is a startup script which generally runs once. This particular file is used for commands which run when the normal user logs in. Common uses for .bash_profile are to set environment variables such as PATH, JAVA_HOME, to create aliases for shell commands, and to set the default permissions for newly created files.

Set JAVA_HOME / PATH for single user

Login to your account and open .bash_profile file
$ vi ~/.bash_profile
Set JAVA_HOME as follows using syntax export JAVA_HOME=<path-to-java>. If your path is set to /usr/java/jdk1.5.0_07/bin/java, set it as follows:
export JAVA_HOME=/usr/java/jdk1.5.0_07/bin/java
Set PATH as follows:
export PATH=$PATH:/usr/java/jdk1.5.0_07/bin
Save and close the file. Just logout and login back to see new changes:
$ 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.

Set JAVA_HOME / PATH for all user

You 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 PATH=$PATH:/usr/java/jdk1.5.0_07/bin
export PATH=$PATH:/usr/java/jdk1.5.0_07/bin

Save and close the file.

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 19 comments… read them below or add one }

1 dino 11.13.07 at 5:23 am

I believe this is wrong !

1) In my case I did all you said for .bash_profile, however, running which java still shows old java path (crappy java that comes with linux fedora 7)

2) I tried the same to set /etc/profile but I believe you provided wrong sintax

Could somebody provide correct sintax for seting java path in /etc/profile or whatever as long as my which java will show my newest java?

Thanks

2 Mustafa Buljubasic 11.13.07 at 5:44 am

I have read lots of user posting at various pages and none of them would work. Finally, I found a way to do this correctly and hope this will help to some of you.

Follow the simple steps:

1. To set the environment variables :

echo ‘export JAVA_HOME=/opt/jdk1.5.0_12′ > /etc/profile.d/jdk.sh
echo ‘export PATH=$JAVA_HOME/bin:$PATH’ >> /etc/profile.d/jdk.sh

2. You have to source the file you just created by typing:
source /etc/profile.d/jdk.sh

3. Test if Java environment is successfully installed by typing in this in the shell:
java -version

3 Mustafa Buljubasic 11.13.07 at 6:03 am

I have read lots of posting and none of them worked well. This one did so I decided to try to post it hopefully somebody else wont have to get this frustrated to get such a simple thing done.

1. To set the environment variables:

echo ‘export JAVA_HOME=/opt/jdk1.5.0_12′ > /etc/profile.d/jdk.sh
echo ‘export PATH=$JAVA_HOME/bin:$PATH’ >> /etc/profile.d/jdk.sh

2. You have to source the file you just created by typing:
source /etc/profile.d/jdk.sh

3. Test if Java environment is successfully installed by typing in this in the shell:
$ java -version
java version “1.6.0_03″
Java(TM) SE Runtime Environment (build 1.6.0_03-b05)
Java HotSpot(TM) Server VM (build 1.6.0_03-b05, mixed mode)

You can also use which java to test:
$ which java
/usr/java/jdk1.6.0_03/bin/java

4 Mustafa Buljubasic 11.13.07 at 6:11 am

my last reply is using mistakenly 2 different versions of jdk. Reference to jdk in point 1 and 3 should be the same as well as in the testing part

5 Mustafa Buljubasic 11.13.07 at 6:16 am

My last 2 postings use 2 different jdk by accident. References to jdk should be the same.

For the sake of consistance, please use (of course substitute your java directory in place of mine /usr/lib/java/jdk….):
echo ‘export JAVA_HOME=/usr/lib/java/jdk1.6.0_03′ > /etc/profile.d/jdk.sh
istead of
echo ‘export JAVA_HOME=/opt/jdk1.5.0_12′ > /etc/profile.d/jdk.sh

My appologies.

6 Ado 01.29.08 at 9:21 am

Mustafa-pasa,

bas se nesto gnjavim sa serverom ali ovaj tvoj post je dosao k’o kec na desetku.

Thanks for your post, it does exactly what it says on the tin. Just what I needed.

7 mohammed 02.18.08 at 12:35 am

I use the IDE 6 with netbeans to create a simple application and I build it and when I navigate to its dist location through the terminal and type:
>> java -jar addition.jar
it gives me this message could you please help me to solve this problem:
Exception in thread “main” java.lang.NoClassDefFoundError: javax/swing/GroupLayout$Group

8 ash 04.24.08 at 10:34 am

i need to run my application with jdk,jmf and jakarta tomcat hw do i go about setting the environment variables?

9 rupert 06.13.08 at 10:39 pm

I think it’s worth mentioning that on most Linux systems, there is a convenient facility to manage different java implementations – its called “alternatives” – depending on the version of your system it may be:
/usr/sbin/alternatives –config java
or
sudo update-alternatives –config java

10 rupert 06.14.08 at 12:25 am

Rather than log out and back in you can run your new bash profile with a single period:
$ cd
$ . .bash_profile

11 Mustafa Buljubasic 12.14.08 at 7:49 pm

How to install JDK (Java Development Kit) on Linux, In my case Fedora 10?

1. Log in as root:
su-
type in your password and the prompt will change from $: to #:

2. Download JDK from here:
http://java.sun.com/javase/downloads/index.jsp
As of time of this post, the most current JDK was:
Java SE Development Kit (JDK) 6 Update 11.
Select file jdk-6u11-linux-i586.rpm.bin to download it by providing your operation system and multilanguage.

3. Go to directory where you downloaded the file (In my case it is Download directory in /home/username/Download/)
The file is called jdk-6u11-linux-i586.rpm.bin

4. Change mode of this file so you can execute it by issuing:
chmod 755 jdk-6u11-linux-i586.rpm.bin

5. run the file by issuing:
./jdk-6u11-linux-i586.rpm.bin
This will show acceptance agreement, press untill you reach end they type “yes” and press and the installation will start.

6. When installation completes, you will need to find the actuall location of your JDK and make Fedora accept your choice. You can do this by issuing:
updatedb;locate javac |grep bin

This will list several options (at least two, the default one shipped with fedora, and the one you downloaded from Java Sun in step 2 above. In my case, I get:
[root@DRACHE Download]# locate javac |grep bin
/usr/bin/javac
/usr/java/jdk1.6.0_10/bin/javac
/usr/java/jdk1.6.0_11/bin/javac –this is what we downloaded in step 2 above, we want to make make Fedora recognaze this jdk.

7. To make Fedora recognaze your jdk (JVM), use alternatives command and issue following 3 commands one after another:
alternatives --install /usr/bin/java java /usr/java/jdk1.6.0_11/bin/java 100
alternatives --install /usr/bin/jar jar /usr/java/jdk1.6.0_11/bin/jar 100
alternatives --install /usr/bin/javac javac /usr/java/jdk1.6.0_11/bin/javac 100

These 3 commands set your java, jar and javac commands. You can use same to set other java executables if you want.

8. Configure alternatives to use the jdk you downloaded above in step 2 rather than the java shipped with Fedora by issuing:
/usr/sbin/alternatives --config java

This will present you with at least 2 options (one is the default jdk shipped with Fedora, other is jdk you downloaded in step 2 above). In my case, I have somehting like this but in your case, this can look different:
/usr/sbin/alternatives --config java

There are 6 programs which provide ‘java’.

  Selection    Command
-----------------------------------------------
   1           /usr/lib/jvm/jre-1.6.0-openjdk.x86_64/bin/java
   2           /usr/lib/jvm/jre-1.5.0-gcj/bin/java
*  3           /usr/java/default/bin/java
 + 4           /usr/java/jdk1.6.0_11/bin/java
   5           /usr/java/jdk1.6.0_11/bin/jar
   6           /usr/java/jdk1.6.0_11/bin/javac

Enter to keep the current selection[+], or type selection number:

9. Choose the one you downloaded in step 2 above and press . In my case, that is option 4.

10. repeat the same for jar and javac command as:
/usr/sbin/alternatives --config jar
/usr/sbin/alternatives --config javac

11. Issue:
java -version
and you will see something like this:

[dino@DRACHE Download]$ java -version
java version “1.6.0_11″
Java(TM) SE Runtime Environment (build 1.6.0_11-b03)
Java HotSpot(TM) Server VM (build 11.0-b16, mixed mode)

Now you are done. Hope this helps :)
Mustafa Buljubasic

Thanks to article from Angsuman Chakraborty from August 7th, 2007

Useful links on how to install JDK, Adobe Flash Player, MP3 players, Media Players, Java Runtime, and much more :
http://blog.taragana.com/index.php/archive/how-to-install-jdk-6-java-se-6-tomcat-in-fedora-core-6-fedora-7-in-5-minutes/
http://www.mjmwired.net/resources/mjm-fedora-f10.html#java

12 vishal joshi 01.09.09 at 11:07 am

Dear Mustafa Buljubasic,
thanking YOu so much.
i need one more help.how to install tomcat and how to set environmental variable for that on linux.

13 Rudra 01.20.09 at 10:31 am

You 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 PATH=$PATH:/usr/java/jdk1.5.0_07/bin]
[export PATH=$PATH:/usr/java/jdk1.5.0_07/bin]

14 neha 02.14.09 at 1:45 pm

hi can some one please help me out .. i just installed java 1.6 on REDHAT 5 system do i need 2 set da classpath or just path n javahome is sufficient .. i tried settng all da 3 bt i guess it didnt work .. can ne1 tel me wts da correct procedure to do it ..

15 Hermes Costell 04.21.09 at 4:07 pm

Mustafa:

THANK YOU! These step-by-step instructions you’ve posted were exactly what I needed to install the JDK on RHEL 5.x

I am floored that there was no link or mention from Sun’s website on how to do this. Do they actually WANT people to use Java? If so it seems to me that if they provided instructions such as these along with the downloads that it would help their product get out a great deal.

Oh well.

16 Saurabh 04.23.09 at 1:59 pm

Hi I am uing mandriva
I ran

export PATH=$PATH:/usr/lib/jvm/java-1.6.0-sun-1.6.0.06/jre/bin

export JAVA_HOME=/usr/lib/jvm/java-1.6.0-sun-1.6.0.06/jre/bin/java

but got errors saying

: line 234: /usr/lib/jvm/java-1.6.0-sun-1.6.0.06/jre/bin/java/bin/java: Not a directory

Please Help me

17 Nagendra 05.28.09 at 11:02 am

Dear Saurabh,

You have exported full path till bin in JAVA_HOME, Just set export JAVA_HOME=/usr/lib/jvm/java-1.6.0-sun-1.6.0.06/jre and it will work.

18 sasa 06.18.09 at 2:05 pm

Thx to Mustafa.
Your solution has been like Ado said “ko kec na desetku”

sve najbolje

19 Alok Guha 06.24.09 at 10:12 am

Thanks..
it worked.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: , , , , , , , , , , ,

Previous post: How to: Turning off SFTP server under Linux / UNIX cpanel server

Next post: Linux: Find out what kernel drivers (modules) are loaded