Search code examples
javalinuxubuntujvmjava-home

How to correctly set the JAVA_HOME environment variable?


on a tutorial (related to a product) it is shown hot to set the JAVA_HOME environment variable on a Linux Ubuntu system, in this way:

echo "JAVA_HOME=\"/usr/lib/jvm/default-java\"" | sudo tee -a /etc/environment

I have some trivial doubts:

1) What exactly is the JAVA_HOME environment variable and for what is it used?

From what I have understand it is something like a link to the JVM setted into the operating system that is used from the servlet container\application server to know where the JVM is and so it can be used. Is this assertion true or am I missing something? Exist some other purpose of this environment variable?

2) In the tutorial it is shown that the path of this environment variable is:

/usr/lib/jvm/default-java

but into my system I have something different, infact into the /usr/lib/jvm/ path I have not the default-java directory but I have a path like this:

/usr/lib/jvm/java-8-oracle

that contains the following directories:

  • bin
  • db
  • include
  • jre
  • lib
  • man

and some other files.

What is the correct path to use for the settings of my JAVA_HOME environment variable?

3) What exactly does this section of the previous statment:

sudo tee -a /etc/environment

Tnx


Solution

  • 1: You are correct

    2: It should point to the actual root folder of the jvm/jdk install, in this case /usr/lib/jvm/java-8-oracle

    3: tee is a command to output the input both to a file and the command line, that -a option appends it to the file. So essentially it, as a super-user, appends the output of the previous command echo "JAVA_HOME=\"/usr/lib/jvm/default-java\"" to the file /etc/environment but also outputs it to the command line. Output should be, for you, exactly: JAVA_HOME="/usr/lib/jvm/java-8-oracle"