Java Environment Variables

Rupam Yadav Dec 19, 2022 May 14, 2021
  1. What Are Environment Variables and Why We Use It
  2. Set Environment Variables for Java in Windows
  3. Set Environment Variables for Java in macOS and Linux
Java Environment Variables

Today, we will look at an important topic that can be helpful when we install the JDK or Java Development Kit in an operating system. Environment variables usually describe the environment and how things should behave in this environment. There are various variables available, but the most used variable is PATH. We will look deeply into this topic in the following sections.

What Are Environment Variables and Why We Use It

Environment Variables are key/value pairs that specify configurations for various programs in an operating system. Most of the popular operating systems like Windows, macOS, and Linux use it. These values are commonly Strings.

The common use of these variables is when we want to execute a program using a command without going to or writing the whole path to the program. For example, if we want to run a Java program anywhere, w6e may have to first go to the location where Java is located through the command-line or terminal, and then we can use Java.

But if we specify the path to Java in the environment variables, we have to use the program’s name or the command attached to it, and the program will be executed.

Set Environment Variables for Java in Windows

We can install Java or JDK in Windows using the install wizard that comes with it, but we have to set the environment variables manually. In the Windows operating system, we can access the settings for environment variables, edit them, or use the command line.

We will see the steps to set the variables using the command line.

  • First we get the location where Java is installed or the Java that we want to use globally. We can do this easily by using the following command in the command line.
    dir /b /s java.exe


  • Now that we have the path, we use it to set the variables; there are two variables we need to configure that are the JAVA_HOME and PATH variables. To do this, we open the command line or CMD for short, and it is better to run it as administrator.
  • In the command line, we write the following command to set both variables:
    setx JAVA_HOME "C:\Users\User1\.jdks\openjdk-15.0.1"
    setx PATH "C:\Users\User1\.jdks\openjdk-15.0.1\bin\";

    Here setx sets the environment variables while JAVA_HOME and PATH are the variable names, and then comes the value of the variable.

It will set or update the variables. Note that JAVA_HOME uses the path where the whole Java folder is situated, while PATH is the path of the subfolder called \bin where Java.exe and other executables are located.

Set Environment Variables for Java in macOS and Linux

In the macOS and Linux, we can use the following steps to set or update the environment variables for Java:

  • Open the terminal
  • identify the default shell that is being used by the OS using this command:
    echo $SHELL



    This will print the default shell. Note that different versions of the operating system may have different shells; for this tutorial, we use the zsh shell.

  • Open ./zshenv that is used to set environment variables in the zsh shell using the below command:
    nano ~/.zshenv
  • Now add the following content.
    export JAVA_HOME=$(/usr/libexec/java_home)
  • Now to execute the script or the content that we just added, we use the below command:
     source ~/.zshenv
  • At last, to check if the variable was set, we will print JAVA_HOME.
    echo $JAVA_HOME


Author: Rupam Yadav
Rupam Yadav avatar Rupam Yadav avatar

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.


Related Article - Java Environment