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.
Subscribe to our free e-mail newsletter or RSS feed to get all updates.
You can Email this page to a friend.
Related Other Helpful FAQs:
- Display or print UNIX / Linux path ~ $PATH variable
- Ubuntu Linux Install Sun Java Development Kit ( JDK ) and Java Runtime Environment ( JRE )
- Environment variable changing on Linux/FreeBSD
- How To Use Proxy Server To Access Internet at Shell Prompt With http_proxy Variable
- Commonly used UNIX / Linux commands by new user
Discussion on This FAQ
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!
Tags: export_command, export_path, java_executable, JAVA_HOME_variable, java_jdk1, linux_system, PATH_variable, path_variables, set_environment_variables, set_path, startup_script, vi_command



November 13th, 2007 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
November 13th, 2007 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
November 13th, 2007 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
November 13th, 2007 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
November 13th, 2007 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.
January 29th, 2008 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.
February 18th, 2008 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
April 24th, 2008 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?
June 13th, 2008 (4 weeks ago) 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
June 14th, 2008 (4 weeks ago) 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