Which JAVA_HOME

There is a tricky problem about JAVA_HOME. When I try to install android studio on my Linux, the installer reports the following message:

1
tools.jar is not in android studio classpath. Please ensure JAVA_HOME points to JDK rather than JRE.

However, JDK has already been installed and I have added the following lines in /etc/profile to export the variables.

1
2
export $JAVA_HOME=/usr/java/jdk1.7.0_79/
export $PATH=$JAVA_HOME/bin:$PATH

Variable JAVA_HOME is pointing to the JDK, but why it is not working on android studio?

The answer is: the correct place to declare JAVA_HOME is /etc/environment, rather than /etc/profile. Although those two files have similar functions, there is an important different. /etc/environment is used for setting variables for programs which are not started from a shell. Since android studio installer is a set of complex programs, it only relies on /etc/environment to obtain JAVA_HOME variable, so /etc/environment is the place to declare JAVA_HOME.

The solution is easy. Append the following line in /etc/environment and everything works fine!

1
JAVA_HOME=/usr/java/jdk1.7.0_79/


Reference:

http://superuser.com/questions/664169/what-is-the-difference-betwen-etc-environment-and-etc-profile