About Linux FAQ

Browse More FAQs:

How to Set JAVA_HOME / PATH variables Under Linux Bash Profile

Posted by Vivek Gite [Last updated: December 15, 2007]

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:

Discussion on This FAQ

  1. dino Says:

    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 Says:

    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 Says:

    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 Says:

    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 Says:

    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 Says:

    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 Says:

    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 Says:

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

  9. rupert Says:

    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 Says:

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

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!

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

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , , , , , , ,

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.