Tell me more ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

How can we set JAVA_HOME environment variable for OpenJDK?

share|improve this question

3 Answers

up vote 6 down vote accepted

You can set your JAVA_HOME in /etc/profile as Petronilla Escarabajo. 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:

echo $JAVA_HOME
share|improve this answer

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:
    JAVA_HOME=/usr/lib/jvm/java-7-oracle
    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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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