5/21/2006

JAVA_HOME vs java.home

What's the difference between JAVA_HOME and java.home? JAVA_HOME is the JDK install directory, e.g., C:\jdk5. It's meant to be set as an environment variable and referenced in Windows batch files or Unix scripts. I always have it in my Windows Control Panel and .tcsh files,along with other common environment variables. Some Java applications use the name jdk.home for this purpose, which I think is a better name. But JAVA_HOME has been used since the beginning and is now a convention.

java.home is the JRE install directory, e.g., C:\jdk5\jre, or C:\Program Files\Java\jre1.5.0_06. Unlike JAVA_HOME, I never seen java.home as an environment variable. java.home is a build-in Java system property, whose value is the JRE install directory. Since all Java system properties are also exposed as Ant build properties, you can also use ${java.home} in build files.

Would jre.home be a better name? Maybe, but I don't think Sun will change it.

Therefore, 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. Why do we still need JAVA_HOME? Well, there are good reasons:

  • You can add $JAVA_HOME/bin to the beginning of your PATH, to make sure you always invoke the right java program. On Windows, java.exe is duplicated in several places, for instance %JAVA_HOME%\bin\java.exe, $JAVA_HOME\jre\bin\java.exe, and %SystemRoot%\system32\java.exe. Without have $JAVA_HOME/bin at the beginning of the PATH, a java command will always resolve to %SystemRoot%\system32\java.exe, which may not be what you want. The same problem can also exist in Linux/Solaris.

  • 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. So the build-in system property java.home is not sufficient.

    7 comments:

    lanada333 said...

    Hi every one
    That's information is clear and interesting but were I can put the JAVA_HOME path in a (WINDOWS7 or VISTA) to reconice the NetBeans are installed in my system
    Tankyou for all and perdon by my English.

    Anonymous said...

    @lanada333

    Vista: Start -> right click on computer -> Properties -> Advanced System Settings -> Environment Variables -> New.. (in 'User Variables') and you add your new variable.

    VirtualVAT said...

    JAVA_HOME is system environment variable and java.home is JVM property. Either one or another can be used to specify java home directory for the virtual machine.

    jane rechod said...

    I'm new to this - but I need to get my head round it for a work project. So, is the java.home property universal throughout the JVM or just local to where it's declared?

    javahowto said...

    You can say it's universal across teh JVM. That is, after starting the JVM, java.home system property is there until JVM shutdown.

    Sandeep Kumar said...

    Nice tutorial. You can also refer this link to to set environment variables PATH, CLASSPATH and JAVA_HOME for compiling and running of Java applications. http://www.a2ztechguide.com/2011/10/setting-environment-variables-javahome.html

    Odisha Real Estate said...

    This is very informative post. Thank you so much for sharing..
    Real Estate in India