Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've installed a version of Java. How can we set the $JAVA_HOME environment variable correctly?

share|improve this question
up vote 104 down vote accepted

You can set your JAVA_HOME in /etc/profile as Petronilla Escarabajo suggests. But the preferred location for JAVA_HOME or any system variable is /etc/environment.

Open /etc/environment in any text editor like nano or gedit and add the following

JAVA_HOME="/usr/lib/jvm/open-jdk" (java path could be different)

Use source to load the variables, by running this command:

source /etc/environment

Then check the variable, by running this command:

share|improve this answer
I'm getting: JDK Required: 'tools.jar' seems to be not in IDEA classpath. Please ensure JAVA_HOME points to JDK rather than JRE. – anon58192932 Mar 21 '14 at 21:31
@advocate - You should probably post your own question for that. But the clue is in the message; you need to point at a JDK not a JRE. – David Edwards Mar 28 '14 at 14:57
@DavidEdwards Apparently my JDK wasn't even installed :) Installed open JDK 7 with the package manager and all was good! I'm working with a foreign Ubuntu build so wasn't sure the extent to which it was set up. – anon58192932 Mar 28 '14 at 18:46
When i tried to run Android Studio (that has IntelliJ IDEA as a base), i had an error message very similar to @advocate's: "'tools.jar' seems to be not in Android Studio classpath." After fiddling a lot with JAVA_HOME without success, i decided to take a look at, the shellscript that starts Android Studio. As a wild guess, i set JDK_HOME to the same value expected for JAVA_HOME, and voila! It installed without great problems. – Hilton Fernandes Mar 7 '15 at 23:20
how to set multiple paths for different JDKs will work?? – LOG_TAG Nov 7 '15 at 17:13

To set JAVA_HOME environment variable, do the following:

  1. Launch Terminal by pressing Ctrl+Alt+T on your keyboard.
  2. Enter the following command:
    $ gksudo gedit /etc/environment
  3. Depending on where you installed your Java, you will need to provide the full path. For this example, I installed Oracle JDK 7 in the /usr/lib/jvm/java-7-oracle directory.
    Scroll to the end of the file and enter the following:
    export JAVA_HOME
  4. Save your file and exit gedit.
  5. Lastly, reload the system PATH with the following command:
    $ . /etc/environment

The above method will save you the hassle in having to run the commands every time you log in to your computer.

share|improve this answer

If you do not know the path and you only have openJDK installed, you can type update-alternatives --config java and you should find the path. To set the variable you can write JAVA_HOME=<PATH> followed by export JAVA_HOME. Notice there's no space when declaring the variable. To check if the variable is stored you simply type echo $JAVA_HOME to verify.

share|improve this answer
This seems like it would be static. If I remove openjdk-7 and install openjdk-9, won't the JAVA_HOME then point to the wrong place? How can it be made dynamic? – DavidJ Jul 20 at 18:49

protected by Community Mar 8 '15 at 7:48

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.