April 30, 2012

How to set JAVA_HOME environment variable in Ubuntu

Setting the JAVA_HOME variable can be done in several ways.

1. Globally and at System level, i.e. no user is required to be logged in. This is the preferred way if you are configuring a server and planning to use boot strapped program that needs JAVA_HOME variable.
2. Globally and set for all logged in user, i.e. one user is needed to be logged in, to have the JAVA_HOME variable set.
3. User specific. Then the variable is only set when the designated user is logged in.

If you have different installed JDK you might find this blog useful on how to manage different JDK on Ubunut http://magnus-k-karlsson.blogspot.se/2012/03/managing-multiple-java-installation-on.html.


Anonymous said...

type what?

Magnus K Karlsson said...

First open a Terminal (Applications → Accessories → Terminal), then enter:

sudo gedit /etc/environment

Append to the end of the file:

Where is your path to where java is installed on your computer. Hope this helps.

NinjaMonkeyBear said...

Thanks for the reminder of /etc/environment. I had completely forgotten that file.

Also, my admiration in your being able to maintain such a professional reply to such a juvenile comment.

Gary said...

one thing to mention here...
i added the JAVA_HOME in the /etc/environment file. But when i echo $JAVA_HOME, it shows a blank line. why?

Gary said...

hmm.... it works after restarting the OS
sorry :p

Scott Sherrell said...

Thank you very much Magnus.

Anonymous said...

Zen Master Magnus. Thanks bro

Akash Yellappa said...

Just doesnt work. Even after reboot

akashy@ubuntu:~$ echo JAVA_HOME

Anonymous said...

you need




Krishna said...

I am really impressed by the contents of your blog!
I have one problem if you could help me:
Is there any way to check if the environmental variable already exists before writing in the /etc/environment file?

shoyeb said...

thanks a lot!!! Magnus

Magnus K Karlsson said...

To check if a variable use echo comman.


Anonymous said...

You are the one, Mr. Karlsson, thank you very much.