How to Solve Error Message Environment Variable not defined when defining an Environment Variable using Command Prompt in Microsoft Windows
Introduction
This is an article where the main focus is exactly clear as it exist in the title of this article. Actually, there is an error appear upon defining or setting an environment variable using Command Prompt in a device running using Microsoft Windows. The error usually occurs because of the incorrect format or syntax pattern of the command. So, by correcting the format or syntax pattern of the command will lead to the success of the command execution. In the end, it will solve the error problem. Actually, this article is part of another article titled ‘How to Set Environment Variable Temporarily using Command Prompt in Microsoft Windows’ in this link. That article is showing how to set or define an environment variable temporarily.
Solve Error Message Environment Variable not defined when defining an Environment Variable using Command Prompt in Microsoft Windows
In order to solve the problem, just find the correct or the suitable solution. In this case, the correct or the suitable solution is just finding the correct format or the correct syntax pattern. Actually, the correct format or syntax pattern for defining an environment variable temporarily exist as follows :
set ENVIRONMENT_VARIABLE_NAME="ENVIRONMENT_VARIABLE_VALUE"
Using the above format or syntax pattern, it will definitely succeed on defining a temporarily environment variable. Actually, the following are the sequence or steps in order which lead to the appearance of the error message :
-
It obviously start from running Command Prompt. Below is normally the appearance of the Command Prompt :
Microsoft Windows [Version 10.0.22631.4037] (c) Microsoft Corporation. All rights reserved. C:\Users\Personal>
-
After that, there are several attempts for setting or defining the environment variable. In this case, it is using an example of JAVA_HOME environment variable. Below is the first attempt which is generating the error message :
Microsoft Windows [Version 10.0.22631.4037] (c) Microsoft Corporation. All rights reserved. C:\Users\Personal> set JAVA_HOME "C:\Program Files\Java\jdk-17.0.2" Environment variable JAVA_HOME "C:\Program Files\Java\jdk-17.0.2" not defined C:\Users\Personal>
The above error message appear because of there is a missing part from the format or syntax pattern for setting or defining the environment variable. It is missing the assignment sign (‘=’). It should be set JAVA_HOME=”C:\Program Files\Java\jdk-17.0.2″
-
Another attempt which is also generating the same error message using another format or syntax pattern for setting or defining JAVA_HOME environment variable exist as follows :
Microsoft Windows [Version 10.0.22631.4037] (c) Microsoft Corporation. All rights reserved. C:\Users\Personal> set %JAVA_HOME% "C:\Program Files\Java\jdk-17.0.2" Environment variable %JAVA_HOME% "C:\Program Files\Java\jdk-17.0.2" not defined C:\Users\Personal>
For the above error message appear, it is actually combining the previous error missing an assignment sign along with the variable name. Setting or defining an environment variable does not need the sign ‘%’ in the beginning and in the end of the variable name. It just needed when it comes to print the environment variable itself.
So, the correct version of setting or defining the JAVA_HOME environment variable is as follows :
Microsoft Windows [Version 10.0.22631.4037] (c) Microsoft Corporation. All rights reserved. C:\Users\Personal> set %JAVA_HOME%="C:\Program Files\Java\jdk-17.0.2" C:\Users\Personal>
Take a look at the content on the following video to see what it looks like :