-1

I had an issue with my Maven CLI not finding the JDK path and I partially resolved it using nocodib's answer (https://stackoverflow.com/a/59035903/18021470) which used the following command:

set JAVA_HOME=%PROGRAMFILES%\Java\jdk-11.0.2

When I ran it, my maven cli could find %JAVA_HOME%, but only on the specific cmd instance. If I closed the cmd, the same issue of my maven cli not finding the jdk would arise.

I also tried using setx instead of set as per another suggestion on the issue, but the code didn't work.

Edit:

Env variable and cmd

1 Answers1

1

set will only set the variable for the lifetime of the current cmd session, not permanently.

setx will modify the variable permanently for future sessions, but not for your current session. (So if you ran setx and then tried your maven command, it still wouldn't be set ).

If you want to have it happen in both the current session and future sessions, you could run both the set and setx command. It would completely depend on your use case.

For understanding set and setx more in depth, please reference: https://superuser.com/a/916652/943436

jhrabi
  • 309
  • 2
  • 16
  • __ATTENTION: DO NOT USE THE COMMAND SETX TO REDEFINE THE SYSTEM OR USER ENVIRONMENT VARIABLE `PATH`!__ It is an absolute NO GO - NEVER EVER to redefine __system__ or __user__ environment variable `Path` with command __SETX__ using the string value of the __local__ environment variable referenced with `%Path%`. The result is a __system__ `Path` with all environment variable references expanded, with folder paths of __user__ `Path` appended resulting in duplicates in concatenated __local__ `Path` and perhaps removed folder paths caused by truncation of command __SETX__. – Mofi Jun 02 '22 at 18:40
  • The redefinition of __system__ or __user__ `Path` from within a Windows command prompt window or using a batch file is really difficult, see for example [Why are other folder paths also added to system PATH with SetX and not only the specified folder path?](https://stackoverflow.com/a/25919222/3074564) The batch code in this post is much better, but is nevertheless not 100% fail-safe. See also other good, but not 100% fail-safe [batch file solutions](https://stackoverflow.com/search?q=user%3A3074564+path+setx). – Mofi Jun 02 '22 at 18:45
  • Users should click on Windows __Start__ menu button, type on keyboard `environment` and Windows offers in language of Windows __Edit the system environment variables__ and __Edit environment variables for your account__. Then the user should click on one of the two items to open the dialog window in which the user can modify safely the __system__ and __user__ environment variables. For more details about `Path` management see also [What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?](https://stackoverflow.com/a/41461002/3074564) – Mofi Jun 02 '22 at 18:49