How to Set JAVA_HOME / PATH variables Under Linux Bash Profile

by Vivek Gite on October 10, 2007 · 53 comments

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.

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 53 comments… read them below or add one }

1 dino November 13, 2007

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

Reply

2 Mustafa Buljubasic November 13, 2007

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

Reply

3 NAVEEN SHARMA December 15, 2010

when I type these above script or command my terminal say “Permission denied”.
Tell the answer
Thanks in Advnace

Reply

4 Chandrasekar February 16, 2011

use “sudo ” if you’re using ubuntu.
else try using “su”
Try googling in case both don’t work :D

Reply

5 Mustafa Buljubasic November 13, 2007

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

Reply

6 sandeep February 2, 2011

hay it is working…but how can i set permentally set…the java path
its working only one terminoal only…
i need permentally java path setting
help me

Reply

7 ashwin February 25, 2011

Hi Sandeep,

Setting java class path in Linux:
I have faced the same problem . How to set it permanently. please help me

Reply

8 csr June 3, 2011

+1 Made my day. Thank you.

Reply

9 Mustafa Buljubasic November 13, 2007

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

Reply

10 Mustafa Buljubasic November 13, 2007

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.

Reply

11 CamNhung September 27, 2010

at the terminal. You type 2 comand to apply the env for bath shell.
#source /etc/profile
#/etc/init.d/x11-common restart

Reply

12 Ado January 29, 2008

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.

Reply

13 mohammed February 18, 2008

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

Reply

14 ash April 24, 2008

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

Reply

15 rupert June 13, 2008

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

Reply

16 rupert June 14, 2008

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

Reply

17 Mustafa Buljubasic December 14, 2008

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

Reply

18 zirimu andrew austin June 6, 2011

Thanks Mustafa, It worked like a charm for me , am using RHEL5

Reply

19 kieron August 23, 2011

Thanks, this helped me.

Fedora 14

Reply

20 vishal joshi January 9, 2009

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.

Reply

21 Rudra January 20, 2009

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]

Reply

22 Pratap July 27, 2011

Why do we need to SET path 2 times the same path??

Reply

23 notgzus July 28, 2011

i think he ment:

export JAVA_HOME=$JAVA_HOME:/usr/java/jdk1.6.0_26/bin/java
export PATH=$PATH:/usr/java/jdk1.6.0_26/bin

Reply

24 neha February 14, 2009

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 ..

Reply

25 Hermes Costell April 21, 2009

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.

Reply

26 Saurabh April 23, 2009

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

Reply

27 Nagendra May 28, 2009

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.

Reply

28 sasa June 18, 2009

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

sve najbolje

Reply

29 Alok Guha June 24, 2009

Thanks..
it worked.

Reply

30 Anuvrat Parashar October 1, 2009

I am using Mandriva Spring 09 and am trying to install IBM WebSphere and Eclipse on it. Eclipse runs fine but WebSphere says something goes wrong and does not start up. OpenJDK gets installed by default and JRE_HOME is set to the one for OpenJDK.
I think that the trouble is because WebSphere does not support OpenJDK. I tried the above methods to export JRE_HOME=/usr/java/jre1.6.0_14/bin but it does not seem to work.
especially when i ran source ~/.bash_profile it gave this error:

bash: TMOUT: readonly variable

help me out.

Reply

31 Prakash May 20, 2010

I installed jre-7 beat onto /opt .I appended the PATH of bin of java to /etc/environment variable and it works.

Reply

32 Arivillathamalayali May 20, 2010

java_vm throws out error “java_vm process: could not find Java VM symbols” ,Any Idea how to set PLUGIN_HOME variable for a jre 7 install(manually installed from jar archive)

Reply

33 ChromeFan May 23, 2010

Hi , set JAVA_HOME and PATH variables for every user under my Linux system(ubuntu 10.04)
open profile
$sudo vi /etc/profile;
insert into last line:
#set java environment
JAVA_HOME=/usr/lib/jvm/java-6-sun // I am using jdk1.6.0.20
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
PATH=$PATH:$JAVA_HOME/bin
export JAVA_HOME
export CLASSPATH
export PATH
export GDK_NATIVE_WINDOWS=true #this is for setting eclipse
#env GDK_NATIVE_WINDOWS=1 #me too!
close and save it
#shell
source /etc/profile //let profile work
reboot
Test if Java environment is successfully installed by typing in this in the shell:
echo $JAVA_HOME
echo $PATH

Reply

34 Khan June 18, 2010

Hi ChromeFan, can u please help me in setting up theh enviromental variables in php.ini file as well.
thanks

Reply

35 andy August 18, 2010

I also like to learn how to change environment variables, like CLASSPATH and LD_LIBRARY_PATH in php.ini.
thanks!

Reply

36 Dev September 6, 2010

hi…to everybdy, i have two versions of java installed on my system jdk1.5 and jdk 1.6. Now i want to make my jdk1.6 as the default version. I do not want to overwrite jdk 1.5. So plz send me the proceedure to solve this issue.

Reply

37 Dev September 6, 2010

I want to implement all this on linux(fedora).

Reply

38 Yogesh September 16, 2010

Vivek, I’m waiting for a tomcat howto :-)

Reply

39 Dev September 17, 2010

hi to evrybdy…dear yogesh dere is nothing much typical to wait for a tomcat..if u wan to set this on ur system then just set the path variable in /etc/bashrc file for all users..actually in my case problem is that i want to implement this for my server.so plz send a correct information rather than any funny remarks.

Reply

40 Yogesh September 17, 2010

@Dave, with ‘tomcat howto’ I mean a complete step by step guide to deploy tomcat app server and integrate it with existing apache with the help of mod_jk. And not ‘just setting the path variable in /etc/bashrc’ as you said. Setting path is something which I have learnt in my nursery rhymes.
By the way the comment I posted was for site owner Vivek Gite and not you. Please read it again and don’t comment until you are very sure about it!!

Reply

41 Dev September 20, 2010

oh great if u’ve learnt dis in ur nuresery rhymes den def u would be the right person who can help me to solve this..sory for misunderstnding. Actualy i m trying to set this path for long so ..

Reply

42 Dev September 20, 2010

oh great if u’ve learnt dis in ur nuresery rhymes den def u would be the right person who can help me to solve this..sory for misunderstnding. Actualy i m trying to set this path for long so.

Reply

43 soothsayer October 6, 2010

I dont know what everyone else is saying. I just read the first couple of posts from people and found some people having issues with this.. However I just wanted to thank the person who wrote this article at the first place. The instructions worked perfect for me.. thanks a lot.

Reply

44 Shanaka November 5, 2010

Thanks This is very useful..

Reply

45 Karthigayan February 23, 2011

Thanks. This is excellent. This was very useful. Thank Musthafa

Reply

46 H.Martin March 17, 2011

This does not work!

Reply

47 Mohammed April 20, 2011

Dear all,

Can anyone help me?

I’ve installed jdk 1.6 update 24 and while opening appliciton its downloadin jdk 1.5,

from backend its showing 1.6 update 24, i’ve tried multiple noluck.

Regards,
Fareed.

Reply

48 haasdas May 14, 2011

I came across this:

sudo update-java-alternatives -s java-6-openjdk
(ubuntu 10.10 for me)
It seems to do a lot of stuff automatically!

Hope it helps some people out there

Reply

49 alsat June 13, 2011

I installed sun-java from synaptic package manager and to initialize these variables i did as explained above..
bt i thnk dr z sth wrong wid it.. i cannot start websphere because it returnd an error wid dese variables..
Can any1 tel me wts d proper way of setting these variables and path and in what all files so that it is set for all users system wide?
thnx!

Reply

50 RT July 27, 2011

I keep getting a dependency problem when trying to install tomcat6 on rhel5:
omcat6-6.0.29-1.jpp5.noarch from jpackage-generic-updates has depsolving problems
–> Missing Dependency: java is needed by package tomcat6-6.0.29-1.jpp5.noarch (jpackage-generic-updates)
tomcat6-6.0.29-1.jpp5.noarch from jpackage-generic-updates has depsolving problems
–> Missing Dependency: java is needed by package tomcat6-6.0.29-1.jpp5.noarch (jpackage-generic-updates)
Error: Missing Dependency: java is needed by package tomcat6-6.0.29-1.jpp5.noarch (jpackage-generic-updates)

But, java is installed correctly as far as I can see. java -version returns
java version “1.6.0_26″
Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02, mixed mode)

Reply

51 RT July 27, 2011

environment variables are setup correctly

env returns:

JAVA_HOME=usr/java/jdk1.6.0_26
PATH=usr/java/jdk1.6.0_26/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/admin/bin

Reply

52 Sam November 7, 2011

Linux Mint 10.10 (should work the same in Ubuntu 10.10)
By default you should be able to type:
$ which java
and get:
/usr/bin/java

/usr/bin/java is actually a symbolic link to /usr/lib/jvm/default-java
If you try to set /usr/bin/java as JAVA_HOME, you will have problems.
As a workaround, you should be able to set JAVA_HOME successfully using:
export JAVA_HOME=/usr/lib/jvm/default-java
export PATH=$PATH:$JAVA_HOME/bin

Reply

53 Pablo February 2, 2012

Você sauvou minha vida! Obrigado!

You sauve my life! Thank you!

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">




Previous post:

Next post: