-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

  • Please read [What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?](https://stackoverflow.com/questions/41454769/) The last chapter __How to modify system or user PATH?__ is for users like you not knowing how `Path` is managed by Windows and how a user like you can safely modify the __system__ or __user__ environment variables like `Path` or `JAVA_HOME`. – Mofi Jun 02 '22 at 18:52
  • If I modify the environment variable JAVA_HOME using this method and edit the variable value to be %PROGRAMFILES%\Java\jdk-17.0.2\bin, the maven CLI still fails to identify it. It's only when I use set JAVA_HOME=%PROGRAMFILES%\Java\jdk-17.0.2\bin that it works – SpicyRiceTea Jun 02 '22 at 19:12
  • You have not really read the entire answer. __NO PROCESS CAN MODIFY THE ENVIRONMENT VARIABLES OF AN ALREADY RUNNING PROCESS__. You have to close at least the already opened command prompt window and open a new one. Then at least the new opened command process makes use of `JAVA_HOME`. It is necessary to restart Windows to restart all running processes so that all processes make use of the redefined __system__ environment variable `JAVA_HOME`. For processes running with your account it is enough to sign out to end all those processes and sign in again resulting in starting them again. – Mofi Jun 02 '22 at 19:22
  • I had restarted the cmd back when I posted my last comment, since your last comment I went ahead and restarted my computer, still no cigar. I've edited my question with an image of what my Env Variable window looks like and what the cmd is giving me. Maybe that can shine some light on the problem... – SpicyRiceTea Jun 02 '22 at 23:55
  • The environment variable `JAVA_HOME` seems to be correct defined. You cannot use just `%JAVA_HOME%` in a command prompt window as this is a directory path and not an executable. You can run in a command prompt window `set` to get output __all local__ environment variables or `set java` to get output just the __local__ environment variables of which name begins case-insensitive with `java`. It looks like `JAVA_HOME` is correct defined in __local__ environment of `cmd.exe` although the value is not visible on the image. Why `mvn` outputs the error message must be analyzed further. – Mofi Jun 03 '22 at 05:02
  • I suppose that `mvn` requires finding `java.exe` with environment variable `Path` and while `JAVA_HOME` is defined as __user__ environment variable which is usually defined as __system__ environment variable, the folder path `%ProgramFiles%\Java\jdk-17.0.2\bin` is not in any `Path`. For that reason the execution of just `java` does not work and there must be used `"%JAVA_HOME%\java.exe"` or `"%ProgramFiles%\Java\jdk-17.0.2\bin\java.exe"` to run Java executable from within the command prompt window. So I guess the error message of `mvn` is misleading. – Mofi Jun 03 '22 at 05:07
  • Please edit your question and add the output of following commands as text. __1.__ `set java` __2.__ `set path` __3.__ `reg query HKCU\Environment /v Path` __4.__ `reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path` See [Copy text from a Windows CMD window to clipboard](https://stackoverflow.com/questions/11543578/) and after paste of output text in edit field, reselect the pasted text and press __Ctrl+K__ or click on button __{ }__ to format the text as preformatted text (code block). – Mofi Jun 03 '22 at 05:11
  • By the way: I don't have any Java installed, but I read now on several web pages that the environment variable `JAVA_HOME` must be defined with the path to the folder containing the subfolder `bin` which contains the executables. That means `JAVA_HOME` must be defined in your case with `%ProgramFiles%\Java\jdk-17.0.2` without `\bin` appended as __user__ or as __system__ environment variable and the __user__ or __system__ environment variable `Path` must contain `%JAVA_HOME%\bin` in list of folder paths. Which `path` must contain this folder path depends on where `JAVA_HOME` is defined. – Mofi Jun 03 '22 at 05:21
  • The installer of Java adds usually the `bin` folder path of Java to __system__ environment variable `Path` on installing Java for all users. So we would need the output of the four commands as requested above to know how all the `JAVA*` and `PATH*` environment variables are configured on your PC. – Mofi Jun 03 '22 at 05:24

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
  • 171
  • 1
  • 13
  • __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