How to Set Environment Variable Permanently using Command Prompt in Microsoft Windows
Introduction
This is another article for setting or defining an environment variable in a device. That device is running using Microsoft Windows as its operating system. Furthermore, the purpose for itself is having the value of it permanently in the environment variable using a specific command. Unless, there is another command execution after for changing the value of the environment variable once more. As for doing it temporarily, just in one active session of Command Prompt directly without having to open another session of Command Prompt, read another article. It exist in an article in this link with the title of ‘How to Set Environment Variable Temporarily using Command Prompt in Microsoft Windows’.
Set Environment Variable Permanently using Command Prompt in Microsoft Windows
So, below are the steps of achieving that purpose :
-
First of all, just run Command Prompt as the chosen tool of Command Line Interface. Normally, it will appear as follows :
Microsoft Windows [Version 10.0.22631.4037] (c) Microsoft Corporation. All rights reserved. C:\Users\Personal>
-
Next, type ‘echo %JAVA_HOME%’ to print the value of JAVA_HOME environment variable in Command Prompt as follows : .
Microsoft Windows [Version 10.0.22631.4037] (c) Microsoft Corporation. All rights reserved. C:\Users\Personal> echo %JAVA_HOME% C:\ C:\Users\Personal>
Actually, the content of the value in the above command for printing the JAVA_HOME environment will be not like that in general. Normally, it will print the path or the location of Java program or Java Development Kit (JDK) tool installation. In the above context, it will just print anything which has been defined before.
-
In order to change the value of the JAVA_HOME environment variable permanently, type the following command :
Microsoft Windows [Version 10.0.22631.4037] (c) Microsoft Corporation. All rights reserved. C:\Users\Personal> echo %JAVA_HOME% C:\ C:\Users\Personal> setx JAVA_HOME "C:\Program Files\Java\jdk-17.0.2"
-
Following after, type back to print the value of the JAVA_HOME environment variable in the current active session of the Command Prompt. Unfortunately, the result does not like ‘set JAVA_HOME “C:\Program Files\Java\jdk-17.0.2” which is instantly change the value of the JAVA_HOME environment variable directly in that current active session of the Command Prompt. It is still printing the same value exist in the current active session of the Command Prompt. The result is totally different when executing the command ‘setx JAVA_HOME “C:\Program Files\Java\jdk-17.0.2”. It will keep printing the same value in the current active session of the Command Prompt. Below is the appearance of the execution of the command :
Microsoft Windows [Version 10.0.22631.4037] (c) Microsoft Corporation. All rights reserved. C:\Users\Personal> echo %JAVA_HOME% C:\ C:\Users\Personal> setx JAVA_HOME "C:\Program Files\Java\jdk-17.0.2" SUCCESS: C:\Users\Personal> echo %JAVA_HOME% C:\ C:\Users\Personal>
-
After that, try to print the JAVA_HOME environment again once more. Unfortunately, as it is already mentioned before, the value of the JAVA_HOME environment variable didn’t change at all. In order to prove that it is actually change but the change itself is not directly implemented in the current active session, just try to run another command prompt session. Continue on by trying to print the environment variable on that new command prompt session. Just type the command for printing the JAVA_HOME environment variable one more. The value of the JAVA_HOME environment variable will change. It will appear as follows :
Microsoft Windows [Version 10.0.22631.4037] (c) Microsoft Corporation. All rights reserved. C:\Users\Personal> echo %JAVA_HOME% C:\Program Files\Java\jdk-17.0.2 C:\Users\Personal>
Take a look in the following video for understanding the steps above :