Before setting the environment variable it is necessary to understand why we set the environment variable and why they are crucial for our system? Let’s understand How to set the Java environment variable in Windows?
The Environment variables are global system variables that can be accessed by all the processes/users running on the system. They are useful to store system-wide values such as PATH, OS, PC name, User Name, System Root, HomePath, etc.
The steps to set the Environment variable in Windows are as follows:
- Open the system properties
- Select the Advance System properties
- Select Advanced System Settings
- Select Environment Variables option
- Select New User Variable
- Enter Path Name & Value of JDK
- Click Ok
- Verify the Process
Let’s understand how to set the environment variable in Windows in detail.
First, verify whether the environment variable is set on your machine or not by executing the below command:
javac
If the environment variable is not set on your machine it will throw an error message “ ‘javac’ is not recognized as an internal or external command”. Consider the following output:
Now, Follow the below steps to set the environment variable in Windows:
Step1: Open the System Properties
To open the system properties right-click on This PC and select the properties option.
Step2: Select Advanced System Settings
The next step is to select the Advanced System Settings from the left menu.
Step3: Select Environment Variables option
It will open a dialog window, select the Select Environment Variable option from this window.
Step4: Select New User Variable
Now, select the New user variable from this window.
Step5: Enter Path Name & Value
Type the variable name as PATH andvariable Value.
The Variable can be found from the installed directory of the JDK under the bin folder. Navigate your directory where you have installed Java and copy the bin folder location of the JDK.
Paste this value in the above Variable Value field and click OK to continue.
Step5: Click OK to Finalize
Now, click OK on the main window of the environment variable.
Now, we have successfully set the environment variable in our system. We can save and run the Java program in any location of our system.
Step6: verify the Process
To verify the process run the javac command. It will display the output as follows:
If your command prompt is not recognizing the javac command, restart your system.
Now, we can save and run the Java program anywhere on the system.
check other Java Tutorials.