Tuesday, July 13, 2021

How to set JAVA_HOME environment in Linux, Unix and Windows? Example

JAVA_HOME is a system environment variable that represents the JDK installation directory. When you install JDK in your machine (Windows, Linux, or UNIX) it creates a home directory and puts all its binary (bin), library(lib), and other tools. In order to compile the java program "javac" tool should be in your PATH and in order to get that in PATH we use the JAVA_HOME environment variable. Many tools like ANT and web servers like tomcat use JAVA_HOME to find java binaries. In this article, we will see how to set the JAVA_HOME environment variable in the different operating systems including Windows (Windows 7, Vista, XP) and Linux (Unix).

Setting JAVA_HOME on Windows environment:

How to set JAVA_HOME environment in Linux, Unix and WindowsWindows environments (Windows 7, Vista, XP) are GUI-based operating systems and you can edit the PATH system variable from GUI. you need to define another variable called JAVA_HOME and set the Java installation directory as a value for the JAVA_HOME variable.

Here is a step by step guide:

1) Windows + Pause -- open System property Window
2) Click Advanced --> Environment variables
3) Click New on System Variable section
4) Add Variable Name: JAVA_HOME and Variable value: C:\Program Files\Java\JDK1.6\
5) Add Java into PATH as: %JAVA_HOME%/bin

An important thing to note here is that on the Windows environment (Windows 7, Vista, XP), you have two sections user-defined variables and System Variables. a variable declared in one section is not visible in other sections.

So if you set JAVA_HOME on the user variable section and update PATH on System variable section, you will not be able to get the value of JAVA_HOME and it will be blank. So be careful with this.

And, If you are a Java beginner then I also recommend you go through these Java Beginner courses from Coursera and Udemy to learn Java in a better and more structured way. This is one of the best and up-to-date courses to learn Java online.


Setting JAVA_HOME on Unix or Linux

Setting JAVA_HOME on Unix or Linux environment is more easy and straightforward. you just need to export the JAVA_HOME variable and its value and then you can refer to its value in any shell script. here is an example of how to set JAVA_HOME on Unix and Linux:

1) export JAVA_HOME=/opt/java/jdk_1.6
2) export PATH= ${PATH}:{JAVA_HOME}/bin

Now Java is added to your system PATH by using the JAVA_HOME System variable. you can also check How to set PATH for Java in Linux or Unix for more details on steps and explanations.



JAVA_HOME vs java.home

I have seen Java programmer confusing over JAVA_HOME and java.home, where former represent JDK installation directory later represent JRE installation directory and you know Difference between JDK and JRE. JRE just contains java binaries and doesn't contain other tools which come with JDK and are required for Java development. java.home is also a Java system property and you can access it on java code using  System.getProperty("java.home"). 

Also, all java system properties are exposed as ANT builtin property you can access java.home inside ANT as ${java.home}


That’s all on how to set JAVA_HOME in Windows and Linux. We have also seen differences between JAVA_HOME and java.home system property. Let us know if you come across any other differences between them.


Other Java Tutorials you may like

5 comments:

  1. A small modification :

    To set/export PATH for java-
    export PATH= ${PATH}:${JAVA_HOME}/bin

    check the '$' before {JAVA_HOME}, i.e. ${JAVA_HOME}.

    ReplyDelete
  2. One of the main advantage of using JAVA_HOME variable to point Java installation directory is that :

    1) It's easy to upgrade JDK without affecting your application startup and config file which points to JAVA_HOME. you just need to download new version and make sure your JAVA_HOME points to new version of Java. This is best benefit of using environment variable or links.

    2) JAVA_HOME variable is short and concise instead of full path to JDK installation directory.

    3) JAVA_HOME variable is platform independence i.e. if your startup script uses JAVA_HOME then it can run on Windows and UNIX without any modification, you just need to set JAVA_HOME on respective operating system.

    4) JAVA_HOME is standard, which means other tools which needs Java e.g. Maven, Eclipse can refer this variable without having any knowledge of where Java is physically installed in your machine, which is obviously be different in different user's machine. JAVA_HOME allows everybody to access Java in a common and standard way.

    Let me know if this make sense.

    ReplyDelete
  3. Does Apache Maven also uses $JAVA_HOME to find out Java version to build a project? I am also confused with PATH and JAVA_HOME environment variable, if both contains location of JDK binary, which will take preference?

    ReplyDelete
  4. does setting JAVA_HOME variable is mandatory to run Java programs?

    ReplyDelete