JAVA_HOME is defined as the environment variable explicitly by the systems engineers majorly for the below reasons. Note that java.home is always there inside JVM, whereas JAVA_HOME exists mainly in your shell environment and you may pass it to JVM as a system property.
- First and foremost reason is to ensure that your Java programs pick the right Java.exe. On Systems (Windows/Linux/Solaris), java.exe is duplicated in several places, for instance %JAVA_HOME%binjava.exe, $JAVA_HOMEjrebinjava.exe, and %SystemRoot%system32java.exe. Without having $JAVA_HOME/bin at the beginning of the PATH, a java command will always resolve to %SystemRoot%system32java.exe, which may not be what you want.
- When you have multiple versions of Java installed and want to specify one them as default. On systems where you have both complete JDK as well as JRE installed, using JAVA_HOME you can specify where the JDK/JRE is installed.
- Where built-in system property java.home is not sufficient in cases like some Java application runtime needs to use tools/libraries only available in JDK. For example, web containers need tools.jar in JDK to compile JSP pages, and ejb containers also need to invoke javac/rmic tools in JDK.