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: