If you've already installed the JDK, it's as simple as this:
type locate jdk and search for the path.
Mine was in /usr/local/jdk1.7.0_45/
So I opened up bash_profile with
and added the following line into it.
Also add :
So that when you execute commands like jar -xf somejar.jar, the executables will be recognized.
Then type source ~/.bash_profile to activate the changes you made (basically the whole of bash_profile will be executed again).
That's it! all programs will recognize JAVA_HOME. I had to do this for Gradle to recognize the path. Without the path, it didn't compile Java programs.