Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java JDK Environment Variable Configuration

Java JDK Environment Variable Configuration Tutorial

In order to use the Java Development Kit (JDK) and its tools effectively, you need to configure environment variables on your system. This tutorial will guide you through the process of setting up the JAVA_HOME and PATH environment variables on Windows, macOS, and Linux.

  • Windows

Follow these steps to set up the JAVA_HOME and PATH environment variables on Windows:

a. Find the JDK installation directory

By default, the JDK is installed in the C:\Program Files\Java\ directory. Locate the installation folder, which should be named something like jdk-11.0.2 (depending on the version you have installed).

b. Set up the JAVA_HOME environment variable

  • Right-click on 'My Computer' or 'This PC' and select 'Properties'.
  • Click on 'Advanced system settings'.
  • Click on the 'Environment Variables' button.
  • Under 'System Variables', click on 'New'.
  • For 'Variable name', enter JAVA_HOME.
  • For 'Variable value', enter the path to your JDK installation directory, for example, C:\Program Files\Java\jdk-11.0.2.
  • Click 'OK' to save the changes.

c. Add JAVA_HOME to the PATH variable

  • Under 'System Variables', find the Path variable and click on 'Edit'.
  • Click on 'New' and add %JAVA_HOME%\bin.
  • Click 'OK' to save the changes.
  • macOS

Follow these steps to set up the JAVA_HOME environment variable on macOS:

a. Open the Terminal app.

b. Run the following command to determine the JDK installation path:

/usr/libexec/java_home

c. Add the following lines to your ~/.bash_profile or ~/.zshrc file (depending on which shell you're using), replacing /path/to/jdk with the output from the previous command:

export JAVA_HOME=/path/to/jdk
export PATH=$JAVA_HOME/bin:$PATH

d. Save the changes and restart the Terminal, or run source ~/.bash_profile or source ~/.zshrc to reload the configuration.

  • Linux (Ubuntu, Debian, and other distributions)

Follow these steps to set up the JAVA_HOME and PATH environment variables on Linux:

a. Open the Terminal app.

b. Run the following command to find the JDK installation path:

readlink -f $(which java) | sed "s:/bin/java::"

c. Add the following lines to your ~/.bashrc or ~/.zshrc file (depending on which shell you're using), replacing /path/to/jdk with the output from the previous command:

export JAVA_HOME=/path/to/jdk
export PATH=$JAVA_HOME/bin:$PATH

d. Save the changes and restart the Terminal, or run source ~/.bashrc or source ~/.zshrc to reload the configuration.

After setting up the JAVA_HOME and PATH environment variables, you should be able to use the JDK tools, such as java, javac, and others, from the command line. To test your configuration, open a new terminal and run java -version. You should see the version information for the installed JDK.

  1. Setting up JAVA_HOME environment variable in Java

    JAVA_HOME points to the installation directory of Java Development Kit (JDK). Set it to the root directory of your JDK installation.

    export JAVA_HOME=/path/to/your/jdk
    
  2. Configuring JDK path in environment variables

    Ensure that the bin directory of the JDK is included in the PATH environment variable to make Java executables accessible from any location.

    export PATH=$PATH:$JAVA_HOME/bin
    
  3. Updating PATH for Java JDK in Windows

    In Windows, add the bin directory of the JDK to the PATH variable. You can do this through the system properties or command line:

    set PATH=%PATH%;C:\path\to\your\jdk\bin
    
  4. Setting JAVA_HOME in Linux or macOS

    Edit the shell profile file (e.g., .bashrc or .zshrc) to set JAVA_HOME:

    export JAVA_HOME=/path/to/your/jdk
    export PATH=$PATH:$JAVA_HOME/bin
    

    Then, reload the profile with source ~/.bashrc or source ~/.zshrc.

  5. Adding Java bin directory to PATH

    The bin directory contains essential executables. Add it to the PATH to run Java commands from any location.

    export PATH=$PATH:$JAVA_HOME/bin
    
  6. Environment variables for Java development

    Additional environment variables like CLASSPATH can be configured for Java development. For example:

    export CLASSPATH=.:/path/to/your/project/libs
    
  7. Checking JDK version using environment variables

    Verify the JDK version using the java command:

    java -version
    

    It should display information about your installed JDK.