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 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.
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
JAVA_HOME
.C:\Program Files\Java\jdk-11.0.2
.c. Add JAVA_HOME
to the PATH
variable
Path
variable and click on 'Edit'.%JAVA_HOME%\bin
.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.
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.
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
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
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
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
.
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
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
Checking JDK version using environment variables
Verify the JDK version using the java
command:
java -version
It should display information about your installed JDK.