A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/setting-environment-java/ below:

Setting up Environment Variables For Java - Complete Guide to Set JAVA_HOME

Setting up Environment Variables For Java - Complete Guide to Set JAVA_HOME

Last Updated : 09 May, 2025

In the journey to learning the Java programming language, setting up environment variables for Java is essential because it helps the system locate the Java tools needed to run the Java programs. Now, this guide on how to setting up environment variables for Java is a one-place solution for Mac, Windows, and Linux users. Here we have discussed the step-by-step process to set the Environment variable in all three major OS.

Before setting up the environment variables, the Java Development Kit (JDK) needs to be installed on your system and you must know the JDK installation directory on your system.

To set Java Environment Variables, you need to set,

Setting the Environment Variables in Windows Step 1: Download JDK Step 2: Set JAVA_HOME Set JAVA_HOME Variable name Step 3: Update the Path Variable Update the Path Variable System Variables JDK's Step 4: Check the Java Version

java --version

java --version

Javac -version

Javac -version Setting up Environment Variables for Java in Linux

If you are Linux user then find the complete solution to setting up environment variables for Java on Linux operating system.

Step 1: Downlaod and Install JAVA JDK

After installing the JDK in Linux, We need to configure the system so that it can recognize Java.

Step 2: Open Linux Terminal

export JAVA_HOME=/path/to/your/jdk

Step 3: Update Path

export PATH=$PATH:$JAVA_HOME/bin

Step 4: Change Environment Variable Permanent

The above changes are temporary. To make the environment variable changes permanent, you need to add the export commands to your shell’s configuration file. Depending on the shell you are using.

Add the following commands at the end of the file.

export JAVA_HOME=/path/to/your/jdk

export PATH=$PATH:$JAVA_HOME/bin

After saving the file, run the following command to apply the changes:

source ~/.bashrc

This will set your JAVA_HOME and PATH variables on Linux permanently.

Step 5: Verify the Installation

echo $JAVA_HOME

Step 6: Check Updated Path

echo $PATH

It should output the path of the bin directory to your JDK. If the output is empty that means the PATH environment variable is not set properly. Then write java --version and javac -version to verify the installation.

Setting up Environment Variables for Java in macOS

If you are Mac user then find the complete solution to setting up environment variables for Java on macOS operating system.

Step 1: Configure the System Step 2: Set up the Java Home

Add the following commands at the end of the file:

export JAVA_HOME=$(/usr/libexec/java_home)

export PATH=$JAVA_HOME/bin:$PATH

Step 3: Change the Base

source ~/.bash_profile

source ~/.zshrc

Step 4: Varify an Installation

echo $JAVA_HOME

echo $PATH

It should output the path of the bin directory to your JDK. If the output is empty that means the PATH environment variable is not set properly.

Then write java --version and javac -version command to verify the installation on macOS.

Step 5: Run the Compiled Java Program

After compiling the Java program, we can run it using the following command:

java HelloWorld

Why Don't We Include .class in the Command?

When running a Java program, the java command expects a class name. It doesn't matter what the file name is, as long as the class with the main() method is specified.

For example: The correct command is java HelloWorld. This will correctly run the program. The command java HelloWorld.class is an incorrect command, this will cause an error because the JVM is looking for a class named HelloWorld.class, not HelloWorld.

After following these steps, your environment variable for Java will be set correctly, and you can easily run Java programs from the terminal without any issues.

Conclusion

Creating a smooth and efficient development workflow requires setting up environment variables for Java. By helping your system locate essential tools like the Java compiler and runtime, these variables simplify many tasks, such as compiling and executing Java programs. A properly configured variable like JAVA_HOME also ensures seamless integration of Java with other frameworks and development tools. Whether you use Windows, macOS, or Linux, you can customize your Java environment to meet your specific needs by following the steps outlined in this guide



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4